Skip to content

Commit 5942788

Browse files
authored
Add Token creation for Enterprises (#399)
1 parent 55b6c5d commit 5942788

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

docs/resources/database.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ output "cqlsh_url" {
6969

7070
### Optional
7171

72-
- `db_type` (String) Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.
72+
- `db_type` (String) Database type. Currently only `vector` is supported. Omit this optional field if you want a regular serverless database.
7373
- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.
7474
- `keyspace` (String) Initial keyspace name. For additional keyspaces, use the astra_keyspace resource. If omitted, Astra will use its default, currently `default_keyspace`
7575
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

docs/resources/token.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ resource "astra_token" "example" {
2525

2626
- `roles` (List of String) List of Role IDs to be assigned to the generated token
2727

28+
### Optional
29+
30+
- `org_id` (String) The UUID of the organization under which the token will be created. If not provided, the token will be created under the organization/enterprise of the token making the request.
31+
2832
### Read-Only
2933

3034
- `client_id` (String) Client id, use as username in cql to connect

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
toolchain go1.22.0
66

77
require (
8-
github.com/datastax/astra-client-go/v2 v2.2.57
8+
github.com/datastax/astra-client-go/v2 v2.2.58
99
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587
1010
github.com/google/uuid v1.6.0
1111
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
101101
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
102102
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
103103
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
104-
github.com/datastax/astra-client-go/v2 v2.2.57 h1:B2AvCRqWOVBs536r42TpWht1Jt1k2OLsBABLAfN0iVw=
105-
github.com/datastax/astra-client-go/v2 v2.2.57/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
104+
github.com/datastax/astra-client-go/v2 v2.2.58 h1:B1fJYtp2Vip6Fbs0+u46clO3fmkqxOG09qVOCHpGTMU=
105+
github.com/datastax/astra-client-go/v2 v2.2.58/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
106106
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A=
107107
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk=
108108
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

internal/provider/resource_database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func resourceDatabase() *schema.Resource {
9191
Default: true,
9292
},
9393
"db_type": {
94-
Description: "Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.",
94+
Description: "Database type. Currently only `vector` is supported. Omit this optional field if you want a regular serverless database.",
9595
Type: schema.TypeString,
9696
Optional: true,
9797
ForceNew: true,

internal/provider/resource_token.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ func resourceToken() *schema.Resource {
3333
Type: schema.TypeString,
3434
},
3535
},
36+
// Optional
37+
"org_id": {
38+
Description: "The UUID of the organization under which the token will be created. If not provided, the token will be created under the organization/enterprise of the token making the request.",
39+
Type: schema.TypeString,
40+
Optional: true,
41+
ForceNew: true,
42+
},
3643
"client_id": {
3744
Description: "Client id, use as username in cql to connect",
3845
Type: schema.TypeString,
@@ -58,6 +65,17 @@ func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, meta inter
5865
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
5966

6067
roles := d.Get("roles").([]interface{})
68+
orgId := d.Get("org_id").(string)
69+
70+
if len(orgId) == 0 {
71+
// no orgId provided, use the one associated with the effective token
72+
currentOrg, err := getCurrentOrgID(ctx, client)
73+
if err != nil {
74+
return diag.Errorf("No Organization ID provided for token creation and an error occurred trying to fetch the Organization associated with the current API token.")
75+
}
76+
// use the org associated with the API token making the call if not provided
77+
orgId = currentOrg
78+
}
6179

6280
rolesList := make([]string, len(roles))
6381

@@ -73,8 +91,9 @@ func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, meta inter
7391

7492
tokenJSON := astra.GenerateTokenForClientJSONRequestBody{
7593
Roles: rolesList,
94+
OrgId: &orgId,
7695
}
77-
resp, err := client.GenerateTokenForClientWithResponse(ctx,
96+
resp, err := client.GenerateAppTokenForClientWithResponse(ctx,
7897
tokenJSON,
7998
)
8099

@@ -136,6 +155,7 @@ func setTokenData(d *schema.ResourceData, tokenMap map[string]interface{}) error
136155
clientID := tokenMap["clientId"].(string)
137156
secret := tokenMap["secret"].(string)
138157
token := tokenMap["token"].(string)
158+
responseOrgId := tokenMap["orgId"].(string)
139159

140160
d.SetId(fmt.Sprintf("%s", clientID))
141161

@@ -148,6 +168,9 @@ func setTokenData(d *schema.ResourceData, tokenMap map[string]interface{}) error
148168
if err := d.Set("token", token); err != nil {
149169
return err
150170
}
171+
if err := d.Set("org_id", responseOrgId); err != nil {
172+
return err
173+
}
151174

152175
return nil
153176
}

0 commit comments

Comments
 (0)