github.com/solo-io/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/openstack/openstack_auth.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "github.com/solo-io/unik/pkg/config" 6 "github.com/rackspace/gophercloud" 7 "github.com/rackspace/gophercloud/openstack" 8 "os" 9 ) 10 11 type openstackHandle struct { 12 AuthClient *gophercloud.ProviderClient 13 Region string 14 } 15 16 // MergeConfWithEnv overrides configuration with environment values (inplace). 17 func MergeConfWithEnv(conf *config.Openstack) { 18 if v := os.Getenv("OS_AUTH_URL"); v != "" { 19 conf.AuthUrl = v 20 } 21 if v := os.Getenv("OS_USER_ID"); v != "" { 22 conf.UserId = v 23 } 24 if v := os.Getenv("OS_USERNAME"); v != "" { 25 conf.UserName = v 26 } 27 if v := os.Getenv("OS_PASSWORD"); v != "" { 28 conf.Password = v 29 } 30 if v := os.Getenv("OS_TENANT_ID"); v != "" { 31 conf.TenantId = v 32 } 33 if v := os.Getenv("OS_TENANT_NAME"); v != "" { 34 conf.TenantName = v 35 } 36 if v := os.Getenv("OS_DOMAIN_ID"); v != "" { 37 conf.DomainId = v 38 } 39 if v := os.Getenv("OS_DOMAIN_NAME"); v != "" { 40 conf.DomainName = v 41 } 42 if v := os.Getenv("OS_REGION_ID"); v != "" { 43 conf.RegionId = v 44 } 45 if v := os.Getenv("OS_REGION_NAME"); v != "" { 46 conf.RegionName = v 47 } 48 } 49 50 // validateCredentials validates presence of required credentials. 51 func validateCredentials(conf *config.Openstack) error { 52 // Validate 53 if conf.AuthUrl == "" { 54 return fmt.Errorf("Argument OS_AUTH_URL needs to be set.") 55 } 56 if conf.UserId == "" && conf.UserName == "" { 57 return fmt.Errorf("Argument OS_USER_ID or OS_USERNAME needs to be set.") 58 } 59 if conf.Password == "" { 60 return fmt.Errorf("Argument OS_PASSWORD needs to be set.") 61 } 62 if conf.TenantId == "" && conf.TenantName == "" { 63 return fmt.Errorf("Argument OS_TENANT_ID or OS_TENANT_NAME needs to be set.") 64 } 65 if conf.RegionId == "" && conf.RegionName == "" { 66 return fmt.Errorf("Argument OS_REGION_ID or OS_REGION_NAME needs to be set.") 67 } 68 69 return nil 70 } 71 72 // getHandle builds openstackHandle object that contains information needed 73 // to obtain any OpenStack API client (e.g. Nova client, Glance client). 74 // NOTE: this function performs a HTTP request to the OpenStack Keystone service 75 func getHandle(conf config.Openstack) (*openstackHandle, error) { 76 if err := validateCredentials(&conf); err != nil { 77 return nil, err 78 } 79 authClient, err := openstack.AuthenticatedClient(gophercloud.AuthOptions{ 80 IdentityEndpoint: conf.AuthUrl, 81 UserID: conf.UserId, 82 Username: conf.UserName, 83 Password: conf.Password, 84 TenantID: conf.TenantId, 85 TenantName: conf.TenantName, 86 DomainID: conf.DomainId, 87 DomainName: conf.DomainName, 88 }) 89 if err != nil { 90 return nil, err 91 } 92 93 region := conf.RegionId 94 if region == "" { 95 region = conf.RegionName 96 } 97 98 return &openstackHandle{ 99 AuthClient: authClient, 100 Region: region, 101 }, nil 102 } 103 104 // getNovaClient returns ServiceClient for OpenStack Nova compute service API. 105 func getNovaClient(handle *openstackHandle) (*gophercloud.ServiceClient, error) { 106 return openstack.NewComputeV2(handle.AuthClient, gophercloud.EndpointOpts{ 107 Region: handle.Region, 108 }) 109 } 110 111 // getGlanceClient returns ServiceClient for OpenStack Glance image service API. 112 func getGlanceClient(handle *openstackHandle) (*gophercloud.ServiceClient, error) { 113 return openstack.NewImageServiceV2(handle.AuthClient, gophercloud.EndpointOpts{ 114 Region: handle.Region, 115 }) 116 }