44 "context"
55 "fmt"
66 "os"
7- "time"
87
98 "github.com/stackitcloud/stackit-sdk-go/core/utils"
109 "github.com/stackitcloud/stackit-sdk-go/services/kms"
@@ -19,63 +18,67 @@ func main() {
1918 // Create a new API client, that uses default authentication and configuration
2019 kmsClient , err := kms .NewAPIClient ()
2120 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 )
2322 os .Exit (1 )
2423 }
25- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
26- defer cancel ()
2724
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 ()
3234 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 )
3537 }
3638
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 ()
4449 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 )
4752 }
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 )
5160 }
52- fmt .Printf ("created key %s\n " , * key .Id )
61+ fmt .Printf ("[kms API] Created key %s\n " , * key .Id )
5362
63+ // List key rings
5464 keyRings , err := kmsClient .ListKeyRingsExecute (ctx , projectId , region )
5565 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 )
5868 }
69+
5970 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 ))
7982 }
8083 }
8184 }
0 commit comments