@@ -24,9 +24,9 @@ import (
2424 "github.com/spf13/cobra"
2525)
2626
27- type GetAllAzureAccounts func () ([]* accounts.AzureServicePrincipalAccount , error )
28- type GetAllAzureWebApps func (account * accounts.AzureServicePrincipalAccount ) ([]* azure.AzureWebApp , error )
29- type GetAllAzureWebAppSlots func (account * accounts.AzureServicePrincipalAccount , app * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error )
27+ type GetAllAzureAccounts func () ([]accounts.IAccount , error )
28+ type GetAllAzureWebApps func (account accounts.IAccount ) ([]* azure.AzureWebApp , error )
29+ type GetAllAzureWebAppSlots func (account accounts.IAccount , app * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error )
3030
3131const (
3232 FlagName = "name"
@@ -85,13 +85,13 @@ func NewCreateOptions(createFlags *CreateFlags, dependencies *cmd.Dependencies)
8585 CreateTargetEnvironmentOptions : shared .NewCreateTargetEnvironmentOptions (dependencies ),
8686 CreateTargetTenantOptions : shared .NewCreateTargetTenantOptions (dependencies ),
8787 WorkerPoolOptions : shared .NewWorkerPoolOptionsForCreateTarget (dependencies ),
88- GetAllAzureAccounts : func () ([]* accounts.AzureServicePrincipalAccount , error ) {
88+ GetAllAzureAccounts : func () ([]accounts.IAccount , error ) {
8989 return getAllAzureAccounts (* dependencies .Client )
9090 },
91- GetAllAzureWebApps : func (account * accounts.AzureServicePrincipalAccount ) ([]* azure.AzureWebApp , error ) {
91+ GetAllAzureWebApps : func (account accounts.IAccount ) ([]* azure.AzureWebApp , error ) {
9292 return getAllAzureWebapps (* dependencies .Client , account )
9393 },
94- GetAllAzureWebAppSlots : func (account * accounts.AzureServicePrincipalAccount , webapp * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error ) {
94+ GetAllAzureWebAppSlots : func (account accounts.IAccount , webapp * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error ) {
9595 return getAllAzureWebAppSlots (* dependencies .Client , account , webapp )
9696 },
9797 }
@@ -115,7 +115,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {
115115
116116 flags := cmd .Flags ()
117117 flags .StringVarP (& createFlags .Name .Value , createFlags .Name .Name , "n" , "" , "A short, memorable, unique name for this Azure Web App." )
118- flags .StringVar (& createFlags .Account .Value , createFlags .Account .Name , "" , "The name or ID of the Azure Service Principal account" )
118+ flags .StringVar (& createFlags .Account .Value , createFlags .Account .Name , "" , "The name or ID of the Azure account" )
119119 flags .StringVar (& createFlags .ResourceGroup .Value , createFlags .ResourceGroup .Name , "" , "The resource group of the Azure Web App" )
120120 flags .StringVar (& createFlags .WebApp .Value , createFlags .WebApp .Name , "" , "The name of the Azure Web App for this deployment target" )
121121 flags .StringVar (& createFlags .Slot .Value , createFlags .Slot .Name , "" , "The name of the Azure Web App Slot for this deployment target" )
@@ -145,6 +145,10 @@ func createRun(opts *CreateOptions) error {
145145 return err
146146 }
147147
148+ if ! isAzureAccount (account ) {
149+ return fmt .Errorf ("account '%s' is not a valid Azure account" , account .GetName ())
150+ }
151+
148152 endpoint := machines .NewAzureWebAppEndpoint ()
149153 endpoint .AccountID = account .GetID ()
150154 endpoint .WebAppName = opts .WebApp .Value
@@ -212,15 +216,15 @@ func PromptMissing(opts *CreateOptions) error {
212216 return nil
213217}
214218
215- func PromptForAccount (opts * CreateOptions ) (* accounts.AzureServicePrincipalAccount , error ) {
216- var account * accounts.AzureServicePrincipalAccount
219+ func PromptForAccount (opts * CreateOptions ) (accounts.IAccount , error ) {
220+ var account accounts.IAccount
217221 if opts .Account .Value == "" {
218222 selectedAccount , err := selectors .Select (
219223 opts .Ask ,
220224 "Select the Azure Account to use\n " ,
221225 opts .GetAllAzureAccounts ,
222- func (p * accounts.AzureServicePrincipalAccount ) string {
223- return ( * p ) .GetName ()
226+ func (p accounts.IAccount ) string {
227+ return fmt . Sprintf ( "%s (%s)" , p .GetName (), getAzureAccountTypeName ( p ) )
224228 })
225229 if err != nil {
226230 return nil , err
@@ -234,11 +238,11 @@ func PromptForAccount(opts *CreateOptions) (*accounts.AzureServicePrincipalAccou
234238 account = a
235239 }
236240
237- opts .Account .Value = account .Name
241+ opts .Account .Value = account .GetName ()
238242 return account , nil
239243}
240244
241- func PromptForWebApp (opts * CreateOptions , account * accounts.AzureServicePrincipalAccount ) error {
245+ func PromptForWebApp (opts * CreateOptions , account accounts.IAccount ) error {
242246 webapps , err := opts .GetAllAzureWebApps (account )
243247 if err != nil {
244248 return err
@@ -309,16 +313,47 @@ func PromptForWebApp(opts *CreateOptions, account *accounts.AzureServicePrincipa
309313 return nil
310314}
311315
312- func getAllAzureWebAppSlots (client client.Client , spAccount * accounts.AzureServicePrincipalAccount , webapp * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error ) {
313- slots , err := azure .GetWebSiteSlots (client , spAccount , webapp )
316+ func isAzureAccount (account accounts.IAccount ) bool {
317+ switch account .GetAccountType () {
318+ case accounts .AccountTypeAzureServicePrincipal ,
319+ accounts .AccountTypeAzureOIDC :
320+ return true
321+ default :
322+ return false
323+ }
324+ }
325+
326+ func getAzureAccountTypeName (account accounts.IAccount ) string {
327+ switch account .GetAccountType () {
328+ case accounts .AccountTypeAzureServicePrincipal :
329+ return "Azure Service Principal"
330+ case accounts .AccountTypeAzureOIDC :
331+ return "Azure OIDC"
332+ default :
333+ return "Unknown"
334+ }
335+ }
336+
337+ func getAllAzureWebAppSlots (client client.Client , account accounts.IAccount , webapp * azure.AzureWebApp ) ([]* azure.AzureWebAppSlot , error ) {
338+ if ! isAzureAccount (account ) {
339+ return nil , fmt .Errorf ("account '%s' is not an Azure account (type: %s)" ,
340+ account .GetName (), account .GetAccountType ())
341+ }
342+
343+ slots , err := azure .GetWebSiteSlots (client , account , webapp )
314344 if err != nil {
315345 return nil , err
316346 }
317347
318348 return slots , nil
319349}
320350
321- func getAllAzureWebapps (client client.Client , account * accounts.AzureServicePrincipalAccount ) ([]* azure.AzureWebApp , error ) {
351+ func getAllAzureWebapps (client client.Client , account accounts.IAccount ) ([]* azure.AzureWebApp , error ) {
352+ if ! isAzureAccount (account ) {
353+ return nil , fmt .Errorf ("account '%s' is not an Azure account (type: %s)" ,
354+ account .GetName (), account .GetAccountType ())
355+ }
356+
322357 sites , err := azure .GetWebSites (client , account )
323358 if err != nil {
324359 return nil , err
@@ -327,23 +362,23 @@ func getAllAzureWebapps(client client.Client, account *accounts.AzureServicePrin
327362 return sites , nil
328363}
329364
330- func getAllAzureAccounts (client client.Client ) ([]* accounts.AzureServicePrincipalAccount , error ) {
365+ func getAllAzureAccounts (client client.Client ) ([]accounts.IAccount , error ) {
331366 allAccounts , err := client .Accounts .GetAll ()
332367 if err != nil {
333368 return nil , err
334369 }
335370
336- var spAccounts []* accounts.AzureServicePrincipalAccount
371+ var azureAccounts []accounts.IAccount
337372 for _ , a := range allAccounts {
338- if s , ok := a .( * accounts. AzureServicePrincipalAccount ); ok {
339- spAccounts = append (spAccounts , s )
373+ if isAzureAccount ( a ) {
374+ azureAccounts = append (azureAccounts , a )
340375 }
341376 }
342377
343- return spAccounts , nil
378+ return azureAccounts , nil
344379}
345380
346- func getAzureAccount (opts * CreateOptions ) (* accounts.AzureServicePrincipalAccount , error ) {
381+ func getAzureAccount (opts * CreateOptions ) (accounts.IAccount , error ) {
347382 idOrName := opts .Account .Value
348383 allAccounts , err := opts .GetAllAzureAccounts ()
349384 if err != nil {
@@ -352,9 +387,12 @@ func getAzureAccount(opts *CreateOptions) (*accounts.AzureServicePrincipalAccoun
352387
353388 for _ , a := range allAccounts {
354389 if strings .EqualFold (a .GetID (), idOrName ) || strings .EqualFold (a .GetName (), idOrName ) {
390+ if ! isAzureAccount (a ) {
391+ return nil , fmt .Errorf ("account %s is not an Azure account (type: %s)" , idOrName , a .GetAccountType ())
392+ }
355393 return a , nil
356394 }
357395 }
358396
359- return nil , fmt .Errorf ("cannot find account %s" , idOrName )
397+ return nil , fmt .Errorf ("cannot find Azure account %s" , idOrName )
360398}
0 commit comments