4
4
"context"
5
5
"fmt"
6
6
"os"
7
- "time"
8
7
9
8
"github.com/stackitcloud/stackit-sdk-go/core/utils"
10
9
"github.com/stackitcloud/stackit-sdk-go/services/kms"
@@ -19,63 +18,67 @@ func main() {
19
18
// Create a new API client, that uses default authentication and configuration
20
19
kmsClient , err := kms .NewAPIClient ()
21
20
if err != nil {
22
- fmt .Fprintf (os .Stderr , "Creating API client: %v\n " , err )
21
+ fmt .Fprintf (os .Stderr , "[kms API] Creating API client: %v\n " , err )
23
22
os .Exit (1 )
24
23
}
25
- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
26
- defer cancel ()
27
24
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 ()
25
+ ctx := context .Background ()
26
+
27
+ keyRing , err := kmsClient .CreateKeyRing (ctx , projectId , region ).
28
+ CreateKeyRingPayload (
29
+ kms.CreateKeyRingPayload {
30
+ Description : utils .Ptr ("a test keyring" ),
31
+ DisplayName : utils .Ptr ("test-keyring" ),
32
+ },
33
+ ).Execute ()
32
34
if err != nil {
33
- fmt .Fprintf (os .Stderr , "cannot create keyring: %v\n " , err )
34
- return
35
+ fmt .Fprintf (os .Stderr , "[kms API] Cannot create keyring: %v\n " , err )
36
+ os . Exit ( 1 )
35
37
}
36
38
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 ()
39
+ // Create a key
40
+ key , err := kmsClient .CreateKey (ctx , projectId , region , * keyRing .Id ).
41
+ CreateKeyPayload (
42
+ kms.CreateKeyPayload {
43
+ Algorithm : kms .ALGORITHM_AES_256_GCM .Ptr (),
44
+ Backend : kms .BACKEND_SOFTWARE .Ptr (),
45
+ Description : utils .Ptr ("A test key" ),
46
+ DisplayName : utils .Ptr ("test-key" ),
47
+ Purpose : kms .PURPOSE_SYMMETRIC_ENCRYPT_DECRYPT .Ptr (),
48
+ }).Execute ()
44
49
if err != nil {
45
- fmt .Fprintf (os .Stderr , "cannot create key: %v\n " , err )
46
- return
50
+ fmt .Fprintf (os .Stderr , "[kms API] Cannot create key: %v\n " , err )
51
+ os . Exit ( 1 )
47
52
}
48
- if err := wait .CreateOrUpdateKeyWaitHandler (ctx , kmsClient , projectId , region , * key .KeyRingId , * key .Id ); err != nil {
49
- fmt .Fprintf (os .Stderr , "failed to create key: %v" , err )
50
- return
53
+ fmt .Printf ("[kms API] Triggered creation of key: %v\n " , * key .Id )
54
+
55
+ // Wait for creation of key
56
+ key , err = wait .CreateOrUpdateKeyWaitHandler (ctx , kmsClient , projectId , region , * key .KeyRingId , * key .Id ).WaitWithContext (ctx )
57
+ if err != nil {
58
+ fmt .Fprintf (os .Stderr , "[kms API] Error when waiting for creation: %v\n " , err )
59
+ os .Exit (1 )
51
60
}
52
- fmt .Printf ("created key %s\n " , * key .Id )
61
+ fmt .Printf ("[kms API] Created key %s\n " , * key .Id )
53
62
63
+ // List key rings
54
64
keyRings , err := kmsClient .ListKeyRingsExecute (ctx , projectId , region )
55
65
if err != nil {
56
- fmt .Fprintf (os .Stderr , "cannot list keyrings: %v\n " , err )
57
- return
66
+ fmt .Fprintf (os .Stderr , "[kms API] Cannot list keyrings: %v\n " , err )
67
+ os . Exit ( 1 )
58
68
}
69
+
59
70
if keyrings := keyRings .KeyRings ; keyrings != nil {
60
- if len (* keyrings ) == 0 {
61
- fmt .Printf ("no keyrings defined\n " )
62
- } else {
63
- for _ , keyring := range * keyrings {
64
- fmt .Printf ("id=%s displayname=%s status=%s\n " , * keyring .Id , * keyring .DisplayName , * keyring .State )
65
- keylist , err := kmsClient .ListKeysExecute (ctx , projectId , region , * key .KeyRingId )
66
- if err != nil {
67
- fmt .Fprintf (os .Stderr , "cannot list keys: %v" , err )
68
- return
69
- }
70
- if keys := keylist .Keys ; keys != nil {
71
- if len (* keys ) == 0 {
72
- fmt .Printf ("no keys\n " )
73
- } else {
74
- for _ , key := range * keys {
75
- fmt .Printf ("key id=%s key name=%s key status=%s\n " , * key .Id , * key .DisplayName , * key .State )
76
- }
77
- }
78
- }
71
+ fmt .Printf ("[kms API] Number of keyrings: %v\n " , len (* keyrings ))
72
+
73
+ for _ , keyring := range * keyrings {
74
+ keylist , err := kmsClient .ListKeysExecute (ctx , projectId , region , * keyring .Id )
75
+ if err != nil {
76
+ fmt .Fprintf (os .Stderr , "[kms API] Cannot list keys: %v\n " , err )
77
+ os .Exit (1 )
78
+ }
79
+
80
+ if keys := keylist .Keys ; keys != nil {
81
+ fmt .Printf ("[kms API] Keys in Keyring %s: %v\n " , * keyring .Id , len (* keys ))
79
82
}
80
83
}
81
84
}
0 commit comments