|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/kms" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + // Specify the project ID |
| 16 | + projectId := "PROJECT_ID" |
| 17 | + region := "eu01" |
| 18 | + |
| 19 | + // Create a new API client, that uses default authentication and configuration |
| 20 | + kmsClient, err := kms.NewAPIClient() |
| 21 | + if err != nil { |
| 22 | + fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err) |
| 23 | + os.Exit(1) |
| 24 | + } |
| 25 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 26 | + defer cancel() |
| 27 | + |
| 28 | + keyRing, err := kmsClient.CreateKeyRing(ctx, projectId, region).CreateKeyRingPayload(kms.CreateKeyRingPayload{ |
| 29 | + Description: utils.Ptr("a test keyring"), |
| 30 | + DisplayName: utils.Ptr("test-keyring"), |
| 31 | + }).Execute() |
| 32 | + if err != nil { |
| 33 | + log.Printf("cannot create keyring: %v", err) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + key, err := kmsClient.CreateKey(ctx, projectId, region, *keyRing.Id).CreateKeyPayload(kms.CreateKeyPayload{ |
| 38 | + Algorithm: kms.ALGORITHM_AES_256_GCM.Ptr(), |
| 39 | + Backend: kms.BACKEND_SOFTWARE.Ptr(), |
| 40 | + Description: utils.Ptr("A test key"), |
| 41 | + DisplayName: utils.Ptr("test-key"), |
| 42 | + Purpose: kms.PURPOSE_SYMMETRIC_ENCRYPT_DECRYPT.Ptr(), |
| 43 | + }).Execute() |
| 44 | + if err != nil { |
| 45 | + log.Printf("cannot create key: %v", err) |
| 46 | + return |
| 47 | + } |
| 48 | + log.Printf("created key %v", key.Id) |
| 49 | + |
| 50 | + keyRings, err := kmsClient.ListKeyRingsExecute(ctx, projectId, region) |
| 51 | + if err != nil { |
| 52 | + log.Printf("cannot list keyrings: %v", err) |
| 53 | + return |
| 54 | + } |
| 55 | + if keyrings := keyRings.KeyRings; keyrings != nil { |
| 56 | + if len(*keyrings) == 0 { |
| 57 | + log.Printf("no keyrings defined") |
| 58 | + } else { |
| 59 | + for _, keyring := range *keyrings { |
| 60 | + log.Printf("id=%s displayname=%s status=%s", *keyring.Id, *keyring.DisplayName, *keyring.State) |
| 61 | + keylist, err := kmsClient.ListKeysExecute(ctx, projectId, region, *key.KeyRingId) |
| 62 | + if err != nil { |
| 63 | + log.Printf("cannot list keys: %v", err) |
| 64 | + return |
| 65 | + } |
| 66 | + if keys := keylist.Keys; keys != nil { |
| 67 | + if len(*keys) == 0 { |
| 68 | + log.Printf("no keys") |
| 69 | + } else { |
| 70 | + for _, key := range *keys { |
| 71 | + log.Printf("key id=%s key name=%s key status=%s", *key.Id, *key.DisplayName, *key.State) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments