|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement/wait" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // Specify the project ID |
| 15 | + projectId := "PROJECT_ID" |
| 16 | + |
| 17 | + // Create a new API client, that uses default authentication and configuration |
| 18 | + client, err := serviceenablement.NewAPIClient( |
| 19 | + config.WithRegion("eu01"), |
| 20 | + ) |
| 21 | + if err != nil { |
| 22 | + fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err) |
| 23 | + os.Exit(1) |
| 24 | + } |
| 25 | + |
| 26 | + // List Services |
| 27 | + listServicesResp, err := client.ListServiceStatusExecute(context.Background(), projectId) |
| 28 | + if err != nil { |
| 29 | + fmt.Fprintf(os.Stderr, "Error when calling `ListServices`: %v\n", err) |
| 30 | + } else { |
| 31 | + fmt.Printf("Number of services: %v\n", len(*listServicesResp.Items)) |
| 32 | + } |
| 33 | + |
| 34 | + // Get Service Id from the list of services |
| 35 | + serviceId := (*listServicesResp.Items)[0].ServiceId |
| 36 | + |
| 37 | + getServiceStatusResp, err := client.GetServiceStatus(context.Background(), projectId, *serviceId).Execute() |
| 38 | + if err != nil { |
| 39 | + fmt.Fprintf(os.Stderr, "Error when calling `GetServiceStatus`: %v\n", err) |
| 40 | + } else { |
| 41 | + fmt.Printf("Service state: %s\n", *getServiceStatusResp.State) |
| 42 | + } |
| 43 | + |
| 44 | + // Enable Service |
| 45 | + err = client.EnableService(context.Background(), projectId, *serviceId).Execute() |
| 46 | + if err != nil { |
| 47 | + fmt.Fprintf(os.Stderr, "Error when calling `EnableService`: %v\n", err) |
| 48 | + } |
| 49 | + // Wait for the service to be enabled |
| 50 | + status, err := wait.EnableServiceWaitHandler(context.Background(), client, projectId, *serviceId).WaitWithContext(context.Background()) |
| 51 | + if err != nil { |
| 52 | + fmt.Fprintf(os.Stderr, "Error when waiting for service to be enabled: %v\n", err) |
| 53 | + } else { |
| 54 | + fmt.Printf("Service %q is now enabled\n", *status.ServiceId) |
| 55 | + } |
| 56 | + |
| 57 | + // Disable Service |
| 58 | + err = client.DisableService(context.Background(), projectId, *serviceId).Execute() |
| 59 | + if err != nil { |
| 60 | + fmt.Fprintf(os.Stderr, "Error when calling `DisableService`: %v\n", err) |
| 61 | + } |
| 62 | + // Wait for the service to be disabled |
| 63 | + status, err = wait.DisableServiceWaitHandler(context.Background(), client, projectId, *serviceId).WaitWithContext(context.Background()) |
| 64 | + if err != nil { |
| 65 | + fmt.Fprintf(os.Stderr, "Error when waiting for service to be disabled: %v\n", err) |
| 66 | + } else { |
| 67 | + fmt.Printf("Service %q is now disabled\n", *status.ServiceId) |
| 68 | + } |
| 69 | +} |
0 commit comments