Exemplo n.º 1
0
func TestListServices(t *testing.T) {
	// Create a service client.
	serviceClient := createAuthenticatedClient(t)
	if serviceClient == nil {
		return
	}

	// Use the client to list all available services.
	pager := services3.List(serviceClient, services3.ListOpts{})
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		parts, err := services3.ExtractServices(page)
		if err != nil {
			return false, err
		}

		t.Logf("--- Page ---")
		for _, service := range parts {
			t.Logf("Service: %32s %15s %10s %s", service.ID, service.Type, service.Name, *service.Description)
		}
		return true, nil
	})
	if err != nil {
		t.Errorf("Unexpected error traversing pages: %v", err)
	}
}
Exemplo n.º 2
0
// returns the first found service ID from a given service type
func (ksClient *KeystoneClientImpl) serviceID(serviceType string) (string, error) {
	opts := services.ListOpts{ServiceType: serviceType, PerPage: 1, Page: 1}
	pager := services.List(ksClient.client, opts)
	serviceID := ""
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		serviceList, err := services.ExtractServices(page)
		if err != nil {
			return false, err
		}
		for _, s := range serviceList {
			serviceID = s.ID
		}
		return false, nil
	})
	if err != nil {
		return "", err
	}
	return serviceID, 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
}
Exemplo n.º 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)
}