func TestAuthenticatedClient(t *testing.T) { // Obtain credentials from the environment. ao, err := openstack.AuthOptionsFromEnv() if err != nil { t.Fatalf("Unable to acquire credentials: %v", err) } client, err := openstack.AuthenticatedClient(ao) if err != nil { t.Fatalf("Unable to authenticate: %v", err) } if client.TokenID == "" { t.Errorf("No token ID assigned to the client") } t.Logf("Client successfully acquired a token: %v", client.TokenID) // Find the storage service in the service catalog. storage, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) if err != nil { t.Errorf("Unable to locate a storage service: %v", err) } else { t.Logf("Located a storage service at endpoint: [%s]", storage.Endpoint) } }
func TestTokenValidate(t *testing.T) { client, err := clients.NewIdentityV2Client() if err != nil { t.Fatalf("Unable to obtain an identity client: %v", err) } authOptions, err := openstack.AuthOptionsFromEnv() if err != nil { t.Fatalf("Unable to obtain authentication options: %v", err) } result := tokens.Create(client, authOptions) token, err := result.ExtractToken() if err != nil { t.Fatalf("Unable to extract token: %v", err) } PrintToken(t, token) getResult := tokens.Get(client, token.ID) user, err := getResult.ExtractUser() if err != nil { t.Fatalf("Unable to extract user: %v", err) } PrintTokenUser(t, user) }
func TestTokenAuthenticate(t *testing.T) { client, err := clients.NewIdentityV2UnauthenticatedClient() if err != nil { t.Fatalf("Unable to obtain an identity client: %v", err) } authOptions, err := openstack.AuthOptionsFromEnv() if err != nil { t.Fatalf("Unable to obtain authentication options: %v", err) } result := tokens.Create(client, authOptions) token, err := result.ExtractToken() if err != nil { t.Fatalf("Unable to extract token: %v", err) } PrintToken(t, token) catalog, err := result.ExtractServiceCatalog() if err != nil { t.Fatalf("Unable to extract service catalog: %v", err) } for _, entry := range catalog.Entries { PrintCatalogEntry(t, &entry) } }
func newClient(t *testing.T) *gophercloud.ServiceClient { ao, err := openstack.AuthOptionsFromEnv() th.AssertNoErr(t, err) client, err := openstack.AuthenticatedClient(ao) th.AssertNoErr(t, err) c, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) th.AssertNoErr(t, err) return c }
// NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient // for the OpenStack Identity v3 API. An error will be returned if // authentication or client creation was not possible. func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) { ao, err := openstack.AuthOptionsFromEnv() if err != nil { return nil, err } client, err := openstack.NewClient(ao.IdentityEndpoint) if err != nil { return nil, err } return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{}) }
// NewBlockStorageV1Client returns a *ServiceClient for making calls // to the OpenStack Block Storage v1 API. An error will be returned // if authentication or client creation was not possible. func NewBlockStorageV1Client() (*gophercloud.ServiceClient, error) { ao, err := openstack.AuthOptionsFromEnv() if err != nil { return nil, err } client, err := openstack.AuthenticatedClient(ao) if err != nil { return nil, err } return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) }
// NewIdentityV2AdminClient returns a *ServiceClient for making calls // to the Admin Endpoint of the OpenStack Identity v2 API. An error // will be returned if authentication or client creation was not possible. func NewIdentityV2AdminClient() (*gophercloud.ServiceClient, error) { ao, err := openstack.AuthOptionsFromEnv() if err != nil { return nil, err } client, err := openstack.AuthenticatedClient(ao) if err != nil { return nil, err } return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), Availability: gophercloud.AvailabilityAdmin, }) }
func TestGetToken(t *testing.T) { client, err := clients.NewIdentityV3UnauthenticatedClient() if err != nil { t.Fatalf("Unable to obtain an identity client: %v") } ao, err := openstack.AuthOptionsFromEnv() if err != nil { t.Fatalf("Unable to obtain environment auth options: %v", err) } authOptions := tokens.AuthOptions{ Username: ao.Username, Password: ao.Password, DomainName: "default", } token, err := tokens.Create(client, &authOptions).Extract() if err != nil { t.Fatalf("Unable to get token: %v", err) } PrintToken(t, token) }
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error { if c.EndpointType != "internal" && c.EndpointType != "internalURL" && c.EndpointType != "admin" && c.EndpointType != "adminURL" && c.EndpointType != "public" && c.EndpointType != "publicURL" && c.EndpointType != "" { return []error{fmt.Errorf("Invalid endpoint type provided")} } if c.Region == "" { c.Region = os.Getenv("OS_REGION_NAME") } // Legacy RackSpace stuff. We're keeping this around to keep things BC. if c.Password == "" { c.Password = os.Getenv("SDK_PASSWORD") } if c.Region == "" { c.Region = os.Getenv("SDK_REGION") } if c.TenantName == "" { c.TenantName = os.Getenv("SDK_PROJECT") } if c.Username == "" { c.Username = os.Getenv("SDK_USERNAME") } // Get as much as possible from the end ao, _ := openstack.AuthOptionsFromEnv() // Make sure we reauth as needed ao.AllowReauth = true // Override values if we have them in our config overrides := []struct { From, To *string }{ {&c.Username, &ao.Username}, {&c.UserID, &ao.UserID}, {&c.Password, &ao.Password}, {&c.IdentityEndpoint, &ao.IdentityEndpoint}, {&c.TenantID, &ao.TenantID}, {&c.TenantName, &ao.TenantName}, {&c.DomainID, &ao.DomainID}, {&c.DomainName, &ao.DomainName}, } for _, s := range overrides { if *s.From != "" { *s.To = *s.From } } // Build the client itself client, err := openstack.NewClient(ao.IdentityEndpoint) if err != nil { return []error{err} } // If we have insecure set, then create a custom HTTP client that // ignores SSL errors. if c.Insecure { config := &tls.Config{InsecureSkipVerify: true} transport := &http.Transport{TLSClientConfig: config} client.HTTPClient.Transport = transport } // Auth err = openstack.Authenticate(client, ao) if err != nil { return []error{err} } c.osClient = client return nil }
// initialize uses our required environment variables to authenticate with // OpenStack and create some clients we will use in the other methods func (p *openstackp) initialize() (err error) { // authenticate opts, err := openstack.AuthOptionsFromEnv() if err != nil { return } provider, err := openstack.AuthenticatedClient(opts) if err != nil { return } // make a compute client p.computeClient, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) if err != nil { return } // make a network client p.networkClient, err = openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ //Name: "neutron", //*** "services can have the same Type but a different Name, which is why [...] Name [is] sometimes needed... but how do I see the available names? Region: os.Getenv("OS_REGION_NAME"), }) if err != nil { return } // we need to know the network pool name *** does this have to be a user // input/config option? Or can it be discovered? p.poolName = os.Getenv("OS_POOL_NAME") // I made this one up, so we'll default to nova if p.poolName == "" { p.poolName = "nova" } p.externalNetworkID, err = networks.IDFromName(p.networkClient, p.poolName) if err != nil { return } // get the details of all the possible server flavors p.fmap = make(map[string]Flavor) pager := flavors.ListDetail(p.computeClient, flavors.ListOpts{}) err = pager.EachPage(func(page pagination.Page) (bool, error) { flavorList, err := flavors.ExtractFlavors(page) if err != nil { return false, err } for _, f := range flavorList { p.fmap[f.ID] = Flavor{ ID: f.ID, Name: f.Name, Cores: f.VCPUs, RAM: f.RAM, Disk: f.Disk, } } return true, nil }) return }