|
5 | 5 | import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse; |
6 | 6 |
|
7 | 7 | class AuthenticationExample { |
8 | | - public static void main(String[] args) { |
9 | | - /////////////////////////////////////////////////////// |
10 | | - // Option 1: setting the paths to service account key (and private key) as configuration |
11 | | - /////////////////////////////////////////////////////// |
12 | | - String SERVICE_ACCOUNT_KEY_PATH = "/path/to/sa_key.json"; |
13 | | - String PRIVATE_KEY_PATH = "/path/to/private_key.pem"; |
14 | | - String SERVICE_ACCOUNT_MAIL = "name-1234@sa.stackit.cloud"; |
| 8 | + public static void main(String[] args) { |
| 9 | + /////////////////////////////////////////////////////// |
| 10 | + // Option 1: setting the paths to service account key (and private key) as configuration |
| 11 | + /////////////////////////////////////////////////////// |
| 12 | + String SERVICE_ACCOUNT_KEY_PATH = "/path/to/sa_key.json"; |
| 13 | + String PRIVATE_KEY_PATH = "/path/to/private_key.pem"; |
| 14 | + String SERVICE_ACCOUNT_MAIL = "name-1234@sa.stackit.cloud"; |
15 | 15 |
|
16 | | - try { |
17 | | - ResourceManagerApi api = new ResourceManagerApi( |
18 | | - new CoreConfiguration() |
19 | | - .serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH) |
20 | | - // Optional: if private key not included in service account key |
21 | | - .privateKeyPath(PRIVATE_KEY_PATH) |
22 | | - ); |
| 16 | + try { |
| 17 | + ResourceManagerApi api = |
| 18 | + new ResourceManagerApi( |
| 19 | + new CoreConfiguration() |
| 20 | + .serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH) |
| 21 | + // Optional: if private key not included in service account key |
| 22 | + .privateKeyPath(PRIVATE_KEY_PATH)); |
23 | 23 |
|
24 | | - /* list all organizations */ |
25 | | - ListOrganizationsResponse response = |
26 | | - api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
| 24 | + /* list all organizations */ |
| 25 | + ListOrganizationsResponse response = |
| 26 | + api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
27 | 27 |
|
28 | | - System.out.println(response); |
29 | | - } catch (Exception e) { |
30 | | - throw new RuntimeException(e); |
31 | | - } |
| 28 | + System.out.println(response); |
| 29 | + } catch (Exception e) { |
| 30 | + throw new RuntimeException(e); |
| 31 | + } |
32 | 32 |
|
33 | | - /////////////////////////////////////////////////////// |
34 | | - // Option 2: setting the service account key (and private key) as configuration |
35 | | - /////////////////////////////////////////////////////// |
36 | | - String SERVICE_ACCOUNT_KEY = "{\n" + |
37 | | - " \"id\": \"uuid\",\n" + |
38 | | - " \"publicKey\": \"public key\",\n" + |
39 | | - " \"createdAt\": \"2023-08-24T14:15:22Z\",\n" + |
40 | | - " \"validUntil\": \"2023-08-24T14:15:22Z\",\n" + |
41 | | - " \"keyType\": \"USER_MANAGED\",\n" + |
42 | | - " \"keyOrigin\": \"USER_PROVIDED\",\n" + |
43 | | - " \"keyAlgorithm\": \"RSA_2048\",\n" + |
44 | | - " \"active\": true,\n" + |
45 | | - " \"credentials\": {\n" + |
46 | | - " \"kid\": \"string\",\n" + |
47 | | - " \"iss\": \"my-sa@sa.stackit.cloud\",\n" + |
48 | | - " \"sub\": \"uuid\",\n" + |
49 | | - " \"aud\": \"string\",\n" + |
50 | | - " \"privateKey\": \"(OPTIONAL) private key when generated by the SA service\"\n" + |
51 | | - " }\n" + |
52 | | - " }"; |
53 | | - String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + |
54 | | - "MIIJQw...ZL+U\n" + |
55 | | - "lm+dqO...xQ8=\n" + |
56 | | - "-----END PRIVATE KEY-----"; |
| 33 | + /////////////////////////////////////////////////////// |
| 34 | + // Option 2: setting the service account key (and private key) as configuration |
| 35 | + /////////////////////////////////////////////////////// |
| 36 | + String SERVICE_ACCOUNT_KEY = |
| 37 | + "{\n" |
| 38 | + + " \"id\": \"uuid\",\n" |
| 39 | + + " \"publicKey\": \"public key\",\n" |
| 40 | + + " \"createdAt\": \"2023-08-24T14:15:22Z\",\n" |
| 41 | + + " \"validUntil\": \"2023-08-24T14:15:22Z\",\n" |
| 42 | + + " \"keyType\": \"USER_MANAGED\",\n" |
| 43 | + + " \"keyOrigin\": \"USER_PROVIDED\",\n" |
| 44 | + + " \"keyAlgorithm\": \"RSA_2048\",\n" |
| 45 | + + " \"active\": true,\n" |
| 46 | + + " \"credentials\": {\n" |
| 47 | + + " \"kid\": \"string\",\n" |
| 48 | + + " \"iss\": \"my-sa@sa.stackit.cloud\",\n" |
| 49 | + + " \"sub\": \"uuid\",\n" |
| 50 | + + " \"aud\": \"string\",\n" |
| 51 | + + " \"privateKey\": \"(OPTIONAL) private key when generated by the SA service\"\n" |
| 52 | + + " }\n" |
| 53 | + + " }"; |
| 54 | + String PRIVATE_KEY = |
| 55 | + "-----BEGIN PRIVATE KEY-----\n" |
| 56 | + + "MIIJQw...ZL+U\n" |
| 57 | + + "lm+dqO...xQ8=\n" |
| 58 | + + "-----END PRIVATE KEY-----"; |
57 | 59 |
|
58 | | - try { |
59 | | - ResourceManagerApi api = new ResourceManagerApi( |
60 | | - new CoreConfiguration() |
61 | | - .serviceAccountKey(SERVICE_ACCOUNT_KEY) |
62 | | - // Optional: if private key not included in service account key |
63 | | - .privateKeyPath(PRIVATE_KEY) |
64 | | - ); |
| 60 | + try { |
| 61 | + ResourceManagerApi api = |
| 62 | + new ResourceManagerApi( |
| 63 | + new CoreConfiguration() |
| 64 | + .serviceAccountKey(SERVICE_ACCOUNT_KEY) |
| 65 | + // Optional: if private key not included in service account key |
| 66 | + .privateKeyPath(PRIVATE_KEY)); |
65 | 67 |
|
66 | | - /* list all organizations */ |
67 | | - ListOrganizationsResponse response = |
68 | | - api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
| 68 | + /* list all organizations */ |
| 69 | + ListOrganizationsResponse response = |
| 70 | + api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
69 | 71 |
|
70 | | - System.out.println(response); |
71 | | - } catch (Exception e) { |
72 | | - throw new RuntimeException(e); |
73 | | - } |
| 72 | + System.out.println(response); |
| 73 | + } catch (Exception e) { |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
74 | 76 |
|
75 | | - /////////////////////////////////////////////////////// |
76 | | - // Option 3: setting the service account key (and private key) as environment variable |
77 | | - /////////////////////////////////////////////////////// |
78 | | - // Set the service account key via environment variable: |
79 | | - // - STACKIT_SERVICE_ACCOUNT_KEY_PATH=/path/to/sa_key.json |
80 | | - // - STACKIT_SERVICE_ACCOUNT_KEY="<content of service account key>" |
81 | | - // |
82 | | - // If the private key is not included in the service account key, set also: |
83 | | - // - STACKIT_PRIVATE_KEY_PATH=/path/to/private_key.pem |
84 | | - // - STACKIT_PRIVATE_KEY="<content of private key>" |
85 | | - // |
86 | | - // If no environment variable is set, fallback to credentials file in "$HOME/.stackit/credentials.json". |
87 | | - // Can be overridden with the environment variable `STACKIT_CREDENTIALS_PATH` |
88 | | - // The credentials file must be a json: |
89 | | - // { |
90 | | - // "STACKIT_SERVICE_ACCOUNT_KEY_PATH": "path/to/sa_key.json", |
91 | | - // "STACKIT_PRIVATE_KEY_PATH": "(OPTIONAL) when the private key isn't included in the Service Account key", |
92 | | - // // Alternative: |
93 | | - // "STACKIT_SERVICE_ACCOUNT_KEY": "<content of private key>", |
94 | | - // "STACKIT_PRIVATE_KEY": "(OPTIONAL) when the private key isn't included in the Service Account key", |
95 | | - // } |
96 | | - try { |
97 | | - ResourceManagerApi api = new ResourceManagerApi(); |
| 77 | + /////////////////////////////////////////////////////// |
| 78 | + // Option 3: setting the service account key (and private key) as environment variable |
| 79 | + /////////////////////////////////////////////////////// |
| 80 | + // Set the service account key via environment variable: |
| 81 | + // - STACKIT_SERVICE_ACCOUNT_KEY_PATH=/path/to/sa_key.json |
| 82 | + // - STACKIT_SERVICE_ACCOUNT_KEY="<content of service account key>" |
| 83 | + // |
| 84 | + // If the private key is not included in the service account key, set also: |
| 85 | + // - STACKIT_PRIVATE_KEY_PATH=/path/to/private_key.pem |
| 86 | + // - STACKIT_PRIVATE_KEY="<content of private key>" |
| 87 | + // |
| 88 | + // If no environment variable is set, fallback to credentials file in |
| 89 | + // "$HOME/.stackit/credentials.json". |
| 90 | + // Can be overridden with the environment variable `STACKIT_CREDENTIALS_PATH` |
| 91 | + // The credentials file must be a json: |
| 92 | + // { |
| 93 | + // "STACKIT_SERVICE_ACCOUNT_KEY_PATH": "path/to/sa_key.json", |
| 94 | + // "STACKIT_PRIVATE_KEY_PATH": "(OPTIONAL) when the private key isn't included in the |
| 95 | + // Service Account key", |
| 96 | + // // Alternative: |
| 97 | + // "STACKIT_SERVICE_ACCOUNT_KEY": "<content of private key>", |
| 98 | + // "STACKIT_PRIVATE_KEY": "(OPTIONAL) when the private key isn't included in the Service |
| 99 | + // Account key", |
| 100 | + // } |
| 101 | + try { |
| 102 | + ResourceManagerApi api = new ResourceManagerApi(); |
98 | 103 |
|
99 | | - /* list all organizations */ |
100 | | - ListOrganizationsResponse response = |
101 | | - api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
| 104 | + /* list all organizations */ |
| 105 | + ListOrganizationsResponse response = |
| 106 | + api.listOrganizations(null, SERVICE_ACCOUNT_MAIL, null, null, null); |
102 | 107 |
|
103 | | - System.out.println(response); |
104 | | - } catch (Exception e) { |
105 | | - throw new RuntimeException(e); |
106 | | - } |
107 | | - } |
| 108 | + System.out.println(response); |
| 109 | + } catch (Exception e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + } |
108 | 113 | } |
0 commit comments