2020-06-11 10:12:41 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
2020-06-11 10:12:41 +02:00
* /
package context
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
2020-08-21 17:24:53 +02:00
"github.com/docker/compose-cli/aci"
2020-09-07 13:23:06 +02:00
"github.com/docker/compose-cli/api/client"
2020-08-21 17:24:53 +02:00
"github.com/docker/compose-cli/context/store"
"github.com/docker/compose-cli/errdefs"
2020-06-11 10:12:41 +02:00
)
2020-07-20 10:48:05 +02:00
func init ( ) {
extraCommands = append ( extraCommands , createAciCommand )
extraHelp = append ( extraHelp , `
Create Azure Container Instances context :
$ docker context create aci CONTEXT [ flags ]
( see docker context create aci -- help )
` )
}
2020-06-11 10:12:41 +02:00
func createAciCommand ( ) * cobra . Command {
2020-07-29 15:12:45 +02:00
var opts aci . ContextParams
2020-06-11 10:12:41 +02:00
cmd := & cobra . Command {
Use : "aci CONTEXT [flags]" ,
Short : "Create a context for Azure Container Instances" ,
Args : cobra . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-06-22 16:35:06 +02:00
return runCreateAci ( cmd . Context ( ) , args [ 0 ] , opts )
2020-06-11 10:12:41 +02:00
} ,
}
2020-07-10 16:39:09 +02:00
addDescriptionFlag ( cmd , & opts . Description )
cmd . Flags ( ) . StringVar ( & opts . Location , "location" , "eastus" , "Location" )
cmd . Flags ( ) . StringVar ( & opts . SubscriptionID , "subscription-id" , "" , "Location" )
cmd . Flags ( ) . StringVar ( & opts . ResourceGroup , "resource-group" , "" , "Resource group" )
2020-06-11 10:12:41 +02:00
return cmd
}
2020-07-29 15:12:45 +02:00
func runCreateAci ( ctx context . Context , contextName string , opts aci . ContextParams ) error {
2020-06-22 16:35:06 +02:00
if contextExists ( ctx , contextName ) {
return errors . Wrapf ( errdefs . ErrAlreadyExists , "context %s" , contextName )
}
contextData , description , err := getAciContextData ( ctx , opts )
if err != nil {
2020-09-16 10:23:23 +02:00
if aci . IsSubscriptionNotFoundError ( err ) {
return errors . New ( "could not find the requested subscription from your Azure login. You might need to specify a tenant ID with docker login azure --tenant-id xxx" )
}
2020-06-22 16:35:06 +02:00
return err
}
return createDockerContext ( ctx , contextName , store . AciContextType , description , contextData )
}
2020-07-29 15:12:45 +02:00
func getAciContextData ( ctx context . Context , opts aci . ContextParams ) ( interface { } , string , error ) {
2020-06-11 10:12:41 +02:00
cs , err := client . GetCloudService ( ctx , store . AciContextType )
if err != nil {
return nil , "" , errors . Wrap ( err , "cannot connect to ACI backend" )
}
2020-07-10 16:39:09 +02:00
return cs . CreateContextData ( ctx , opts )
2020-06-11 10:12:41 +02:00
}