Quickstart
Stay organized with collections
Save and categorize content based on your preferences.
Basic usage of the Service Directory client library.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Go
To learn how to install and use the client library for Service Directory, see
Service Directory client libraries.
// Sample quickstart is a program that uses Cloud Service Directory
// create. delete, and resolve functionality.
package main
import (
"context"
"fmt"
"log"
servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)
func main() {
projectID := "your-project-id"
location := "us-west1"
serviceID := "golang-quickstart-service"
namespaceID := "golang-quickstart-namespace"
endpointID := "golang-quickstart-endpoint"
ctx := context.Background()
// Create a registration client.
registry, err := servicedirectory.NewRegistrationClient(ctx)
if err != nil {
log.Fatalf("servicedirectory.NewRegistrationClient: %v", err)
}
defer registry.Close()
// Create a lookup client.
resolver, err := servicedirectory.NewLookupClient(ctx)
if err != nil {
log.Fatalf("servicedirectory.NewLookupClient: %v", err)
}
defer resolver.Close()
// Create a Namespace.
createNsReq := &sdpb.CreateNamespaceRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
NamespaceId: namespaceID,
}
namespace, err := registry.CreateNamespace(ctx, createNsReq)
if err != nil {
log.Fatalf("servicedirectory.CreateNamespace: %v", err)
}
// Create a Service.
createServiceReq := &sdpb.CreateServiceRequest{
Parent: namespace.Name,
ServiceId: serviceID,
Service: &sdpb.Service{
Annotations: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
}
service, err := registry.CreateService(ctx, createServiceReq)
if err != nil {
log.Fatalf("servicedirectory.CreateService: %v", err)
}
// Create an Endpoint.
createEndpointReq := &sdpb.CreateEndpointRequest{
Parent: service.Name,
EndpointId: endpointID,
Endpoint: &sdpb.Endpoint{
Address: "8.8.8.8",
Port: 8080,
Annotations: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
}
_, err = registry.CreateEndpoint(ctx, createEndpointReq)
if err != nil {
log.Fatalf("servicedirectory.CreateEndpoint: %v", err)
}
// Now Resolve the service.
lookupRequest := &sdpb.ResolveServiceRequest{
Name: service.Name,
}
result, err := resolver.ResolveService(ctx, lookupRequest)
if err != nil {
log.Fatalf("servicedirectory.ResolveService: %v", err)
return
}
fmt.Printf("Successfully Resolved Service %v", result)
// Delete the namespace.
deleteNsReq := &sdpb.DeleteNamespaceRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s",
projectID, location, namespaceID),
}
registry.DeleteNamespace(ctx, deleteNsReq) // Ignore results.
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]