|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/datastax/astra-client-go/v2/astra" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceToken() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: "`astra_token` resource represents a token with a specific role assigned.", |
| 17 | + CreateContext: resourceTokenCreate, |
| 18 | + ReadContext: resourceTokenRead, |
| 19 | + DeleteContext: resourceTokenDelete, |
| 20 | + |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + StateContext: schema.ImportStatePassthroughContext, |
| 23 | + }, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + // Required |
| 27 | + "roles": { |
| 28 | + Description: "Roles for generated token", |
| 29 | + Type: schema.TypeList, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + }, |
| 35 | + }, |
| 36 | + "client_id": { |
| 37 | + Description: "Client id, use as username in cql to connect", |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "secret": { |
| 42 | + Description: "Secret, use as password in cql to connect", |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "token": { |
| 47 | + Description: "Token, use as auth bearer for API calls or as password in combination with the word `token` in cql", |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + |
| 52 | + |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func resourceTokenCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 58 | + client := meta.(*astra.ClientWithResponses) |
| 59 | + |
| 60 | + roles := d.Get("roles").([]interface{}) |
| 61 | + |
| 62 | + rolesList := make([]string, len(roles)) |
| 63 | + |
| 64 | + for k, v := range roles { |
| 65 | + rolesList[k] = v.(string) |
| 66 | + } |
| 67 | + |
| 68 | + tokenJSON := astra.GenerateTokenForClientJSONRequestBody{ |
| 69 | + Roles: rolesList, |
| 70 | + } |
| 71 | + resp, err := client.GenerateTokenForClientWithResponse(ctx, |
| 72 | + tokenJSON, |
| 73 | + ) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } else if resp.StatusCode() >= 400 { |
| 78 | + return diag.Errorf("error adding role to org: %s", resp.Body) |
| 79 | + } |
| 80 | + |
| 81 | + token := (*resp.JSON200).(map[string]interface{}) |
| 82 | + if err := setTokenData(d, token); err != nil { |
| 83 | + return diag.FromErr(err) |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTokenDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 91 | + client := meta.(*astra.ClientWithResponses) |
| 92 | + |
| 93 | + id := d.Id() |
| 94 | + |
| 95 | + clientID, err := parseTokenID(id) |
| 96 | + if err != nil { |
| 97 | + return diag.FromErr(err) |
| 98 | + } |
| 99 | + |
| 100 | + client.DeleteTokenForClient(ctx, astra.ClientIdParam(clientID)) |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func resourceTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 106 | + |
| 107 | + client := meta.(*astra.ClientWithResponses) |
| 108 | + |
| 109 | + id := d.Id() |
| 110 | + |
| 111 | + clientID, err := parseTokenID(id) |
| 112 | + if err != nil { |
| 113 | + return diag.FromErr(err) |
| 114 | + } |
| 115 | + |
| 116 | + token, err := listToken(ctx, client, clientID) |
| 117 | + if err != nil { |
| 118 | + return diag.FromErr(err) |
| 119 | + } |
| 120 | + |
| 121 | + d.SetId(fmt.Sprintf("%s", clientID)) |
| 122 | + if err := d.Set("client_id", token["client_id"]); err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +func setTokenData(d *schema.ResourceData, tokenMap map[string]interface{}) error { |
| 131 | + clientID := tokenMap["clientId"].(string) |
| 132 | + secret:= tokenMap["secret"].(string) |
| 133 | + token := tokenMap["token"].(string) |
| 134 | + |
| 135 | + d.SetId(fmt.Sprintf("%s", clientID)) |
| 136 | + |
| 137 | + if err := d.Set("client_id", clientID); err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + if err := d.Set("secret", secret); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + if err := d.Set("token", token); err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func parseTokenID(id string) (string, error) { |
| 151 | + idParts := strings.Split(strings.ToLower(id), "/") |
| 152 | + if len(idParts) != 1 { |
| 153 | + return "", errors.New("invalid token id format: expected clientID/") |
| 154 | + } |
| 155 | + return idParts[0], nil |
| 156 | +} |
0 commit comments