Skip to content

Commit

Permalink
Add Token creation for Enterprises (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 authored Jun 28, 2024
1 parent 55b6c5d commit 5942788
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/resources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ output "cqlsh_url" {

### Optional

- `db_type` (String) Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.
- `db_type` (String) Database type. Currently only `vector` is supported. Omit this optional field if you want a regular serverless database.
- `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`.
- `keyspace` (String) Initial keyspace name. For additional keyspaces, use the astra_keyspace resource. If omitted, Astra will use its default, currently `default_keyspace`
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ resource "astra_token" "example" {

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

### Optional

- `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.

### Read-Only

- `client_id` (String) Client id, use as username in cql to connect
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.22.0

require (
github.com/datastax/astra-client-go/v2 v2.2.57
github.com/datastax/astra-client-go/v2 v2.2.58
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/datastax/astra-client-go/v2 v2.2.57 h1:B2AvCRqWOVBs536r42TpWht1Jt1k2OLsBABLAfN0iVw=
github.com/datastax/astra-client-go/v2 v2.2.57/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
github.com/datastax/astra-client-go/v2 v2.2.58 h1:B1fJYtp2Vip6Fbs0+u46clO3fmkqxOG09qVOCHpGTMU=
github.com/datastax/astra-client-go/v2 v2.2.58/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M=
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A=
github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func resourceDatabase() *schema.Resource {
Default: true,
},
"db_type": {
Description: "Database type. Currently only `vector` is supported. Omit this optional field if you want a regular severless database.",
Description: "Database type. Currently only `vector` is supported. Omit this optional field if you want a regular serverless database.",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Expand Down
25 changes: 24 additions & 1 deletion internal/provider/resource_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func resourceToken() *schema.Resource {
Type: schema.TypeString,
},
},
// Optional
"org_id": {
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.",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"client_id": {
Description: "Client id, use as username in cql to connect",
Type: schema.TypeString,
Expand All @@ -58,6 +65,17 @@ func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, meta inter
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)

roles := d.Get("roles").([]interface{})
orgId := d.Get("org_id").(string)

if len(orgId) == 0 {
// no orgId provided, use the one associated with the effective token
currentOrg, err := getCurrentOrgID(ctx, client)
if err != nil {
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.")
}
// use the org associated with the API token making the call if not provided
orgId = currentOrg
}

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

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

tokenJSON := astra.GenerateTokenForClientJSONRequestBody{
Roles: rolesList,
OrgId: &orgId,
}
resp, err := client.GenerateTokenForClientWithResponse(ctx,
resp, err := client.GenerateAppTokenForClientWithResponse(ctx,
tokenJSON,
)

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

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

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

return nil
}
Expand Down

0 comments on commit 5942788

Please sign in to comment.