|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/dns" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // Specify the project ID |
| 15 | + projectId := "PROJECT_ID" |
| 16 | + |
| 17 | + // Create a cancellable context |
| 18 | + ctx := context.Background() |
| 19 | + ctx, cancel := context.WithCancel(ctx) |
| 20 | + defer cancel() // This is needed to abort the background routine, to prevent resource leaks |
| 21 | + |
| 22 | + // Create a new API client, that uses key flow and has background token refresh enabled |
| 23 | + dnsClient, err := dns.NewAPIClient( |
| 24 | + config.WithServiceAccountKeyPath("path/to/service/account/key"), |
| 25 | + config.WithBackgroundTokenRefresh(ctx), |
| 26 | + ) |
| 27 | + if err != nil { |
| 28 | + fmt.Fprintf(os.Stderr, "Error when creating API client: %v\n", err) |
| 29 | + } |
| 30 | + |
| 31 | + // The client can then be used as normal, a new token will be fetched whenever the current one is close to expiring |
| 32 | + // Use this for long-running operations (hours) |
| 33 | + for i := 0; i < 10; i++ { |
| 34 | + // Get the DNS zones for your project |
| 35 | + getZoneResp, err := dnsClient.ListZones(context.Background(), projectId).Execute() |
| 36 | + if err != nil { |
| 37 | + fmt.Fprintf(os.Stderr, "Error when calling `ListZones`: %v\n", err) |
| 38 | + } |
| 39 | + fmt.Printf("Number of DNS zones: %v\n", len(*getZoneResp.Zones)) |
| 40 | + |
| 41 | + time.Sleep(time.Hour) |
| 42 | + } |
| 43 | +} |
0 commit comments