Example #1
0
func TestListEndpoints(t *testing.T) {
	// Create a service client.
	serviceClient := createAuthenticatedClient(t)
	if serviceClient == nil {
		return
	}

	// Use the service to list all available endpoints.
	pager := endpoints3.List(serviceClient, endpoints3.ListOpts{})
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page ---")

		endpoints, err := endpoints3.ExtractEndpoints(page)
		if err != nil {
			t.Fatalf("Error extracting endpoings: %v", err)
		}

		for _, endpoint := range endpoints {
			t.Logf("Endpoint: %8s %10s %9s %s",
				endpoint.ID,
				endpoint.Availability,
				endpoint.Name,
				endpoint.URL)
		}

		return true, nil
	})
	if err != nil {
		t.Errorf("Unexpected error while iterating endpoint pages: %v", err)
	}
}
Example #2
0
// finds the URL for a given service ID and availability
func (ksClient *KeystoneClientImpl) endpointURL(serviceID string, availability gophercloud.Availability) (*url.URL, error) {
	opts := endpoints.ListOpts{Availability: availability, ServiceID: serviceID, PerPage: 1, Page: 1}
	pager := endpoints.List(ksClient.client, opts)
	endpointURL := (*url.URL)(nil)
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		endpointList, err := endpoints.ExtractEndpoints(page)
		if err != nil {
			return false, err
		}
		for _, e := range endpointList {
			URL, err := url.Parse(e.URL)
			if err != nil {
				return false, err
			}
			endpointURL = URL
		}
		return false, nil
	})
	if err != nil {
		return nil, err
	}
	return endpointURL, nil
}
// V3EndpointURL discovers the endpoint URL for a specific service using multiple calls against
// an identity v3 service endpoint. The specified EndpointOpts are used to identify a unique,
// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
// criteria and when none do. The minimum that can be specified is a Type, but you will also often
// need to specify a Name and/or a Region depending on what's available on your OpenStack
// deployment.
func V3EndpointURL(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
	// Discover the service we're interested in.
	var services = make([]services3.Service, 0, 1)
	servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
	err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
		part, err := services3.ExtractServices(page)
		if err != nil {
			return false, err
		}

		for _, service := range part {
			if service.Name == opts.Name {
				services = append(services, service)
			}
		}

		return true, nil
	})
	if err != nil {
		return "", err
	}

	if len(services) == 0 {
		return "", gophercloud.ErrServiceNotFound
	}
	if len(services) > 1 {
		return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services)
	}
	service := services[0]

	// Enumerate the endpoints available for this service.
	var endpoints []endpoints3.Endpoint
	endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{
		Availability: opts.Availability,
		ServiceID:    service.ID,
	})
	err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
		part, err := endpoints3.ExtractEndpoints(page)
		if err != nil {
			return false, err
		}

		for _, endpoint := range part {
			if opts.Region == "" || endpoint.Region == opts.Region {
				endpoints = append(endpoints, endpoint)
			}
		}

		return true, nil
	})
	if err != nil {
		return "", err
	}

	if len(endpoints) == 0 {
		return "", gophercloud.ErrEndpointNotFound
	}
	if len(endpoints) > 1 {
		return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
	}
	endpoint := endpoints[0]

	return gophercloud.NormalizeURL(endpoint.URL), nil
}
Example #4
0
func TestNavigateCatalog(t *testing.T) {
	// Create a service client.
	client := createAuthenticatedClient(t)
	if client == nil {
		return
	}

	var compute *services3.Service
	var endpoint *endpoints3.Endpoint

	// Discover the service we're interested in.
	servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"})
	err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
		part, err := services3.ExtractServices(page)
		if err != nil {
			return false, err
		}
		if compute != nil {
			t.Fatalf("Expected one service, got more than one page")
			return false, nil
		}
		if len(part) != 1 {
			t.Fatalf("Expected one service, got %d", len(part))
			return false, nil
		}

		compute = &part[0]
		return true, nil
	})
	if err != nil {
		t.Fatalf("Unexpected error iterating pages: %v", err)
	}

	if compute == nil {
		t.Fatalf("No compute service found.")
	}

	// Enumerate the endpoints available for this service.
	computePager := endpoints3.List(client, endpoints3.ListOpts{
		Availability: gophercloud.AvailabilityPublic,
		ServiceID:    compute.ID,
	})
	err = computePager.EachPage(func(page pagination.Page) (bool, error) {
		part, err := endpoints3.ExtractEndpoints(page)
		if err != nil {
			return false, err
		}
		if endpoint != nil {
			t.Fatalf("Expected one endpoint, got more than one page")
			return false, nil
		}
		if len(part) != 1 {
			t.Fatalf("Expected one endpoint, got %d", len(part))
			return false, nil
		}

		endpoint = &part[0]
		return true, nil
	})

	if endpoint == nil {
		t.Fatalf("No endpoint found.")
	}

	t.Logf("Success. The compute endpoint is at %s.", endpoint.URL)
}