-
Notifications
You must be signed in to change notification settings - Fork 81
SP-991 #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
SP-991 #845
Changes from all commits
0cbcdbf
8d4a428
ea2201f
178e58a
88d2836
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,11 @@ const ( | |
| paramGcpKeyId = "key_id" | ||
| paramAwsRoles = "roles" | ||
|
|
||
| paramValidation = "validation" | ||
| paramPhase = "phase" | ||
| paramSince = "since" | ||
| paramMessage = "message" | ||
|
|
||
| kindAws = "AwsKey" | ||
| kindAzure = "AzureKey" | ||
| kindGcp = "GcpKey" | ||
|
|
@@ -35,14 +40,21 @@ func byokResource() *schema.Resource { | |
| return &schema.Resource{ | ||
| CreateContext: byokCreate, | ||
| ReadContext: byokRead, | ||
| UpdateContext: byokUpdate, | ||
| DeleteContext: byokDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| StateContext: byokImport, | ||
| }, | ||
| Schema: map[string]*schema.Schema{ | ||
| paramAws: awsKeySchema(), | ||
| paramAzure: azureKeySchema(), | ||
| paramGcp: gcpKeySchema(), | ||
| paramDisplayName: { | ||
| Type: schema.TypeString, | ||
| Description: "A human-readable name for the BYOK key.", | ||
| Optional: true, | ||
| }, | ||
| paramValidation: validationSchema(), | ||
| paramAws: awsKeySchema(), | ||
| paramAzure: azureKeySchema(), | ||
| paramGcp: gcpKeySchema(), | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -96,6 +108,38 @@ func gcpKeySchema() *schema.Schema { | |
| } | ||
| } | ||
|
|
||
| func validationSchema() *schema.Schema { | ||
| return &schema.Schema{ | ||
| Type: schema.TypeList, | ||
| Description: "Validation information for the BYOK key.", | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| paramPhase: { | ||
| Type: schema.TypeString, | ||
| Description: "The validation phase of the key (INITIALIZING, VALID, INVALID).", | ||
| Computed: true, | ||
| }, | ||
| paramSince: { | ||
| Type: schema.TypeString, | ||
| Description: "Timestamp when the key entered the current validation phase.", | ||
| Computed: true, | ||
| }, | ||
| paramMessage: { | ||
| Type: schema.TypeString, | ||
| Description: "Optional validation message providing additional details.", | ||
| Computed: true, | ||
| }, | ||
| paramRegion: { | ||
| Type: schema.TypeString, | ||
| Description: "Region information for successfully validated keys.", | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func azureKeySchema() *schema.Schema { | ||
| return &schema.Schema{ | ||
| Type: schema.TypeList, | ||
|
|
@@ -134,6 +178,11 @@ func byokCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) d | |
| c := meta.(*Client) | ||
|
|
||
| createByokKeyRequest := byok.NewByokV1Key() | ||
|
|
||
| // Set display name | ||
| displayName := d.Get(paramDisplayName).(string) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recall that some resources only assign this attribute if it is not empty. Will the server still process the request if name is an empty string ("")?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, an patching with empty works. Tried an example with the provider to be sure: |
||
| createByokKeyRequest.SetDisplayName(displayName) | ||
|
|
||
| _, isAwsKey := d.GetOk(paramAws) | ||
| _, isAzureKey := d.GetOk(paramAzure) | ||
| _, isGcpKey := d.GetOk(paramGcp) | ||
|
|
@@ -246,6 +295,28 @@ func readKeyAndSetAttributes(ctx context.Context, d *schema.ResourceData, meta i | |
| } | ||
|
|
||
| func setKeyAttributes(d *schema.ResourceData, byokKey byok.ByokV1Key) (*schema.ResourceData, error) { | ||
| // Set display name | ||
| if err := d.Set(paramDisplayName, byokKey.GetDisplayName()); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Set validation information if available | ||
| if validation, ok := byokKey.GetValidationOk(); ok { | ||
| validationMap := map[string]interface{}{ | ||
| paramPhase: validation.GetPhase(), | ||
| paramSince: validation.GetSince().Format("2006-01-02T15:04:05.000Z"), | ||
| } | ||
| if message, messageOk := validation.GetMessageOk(); messageOk { | ||
| validationMap[paramMessage] = *message | ||
| } | ||
| if region, regionOk := validation.GetRegionOk(); regionOk { | ||
| validationMap[paramRegion] = *region | ||
| } | ||
| if err := d.Set(paramValidation, []interface{}{validationMap}); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| oneOfKeys := byokKey.GetKey() | ||
|
|
||
| switch { | ||
|
|
@@ -278,6 +349,40 @@ func setKeyAttributes(d *schema.ResourceData, byokKey byok.ByokV1Key) (*schema.R | |
| return d, nil | ||
| } | ||
|
|
||
| func byokUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| if d.HasChangeExcept(paramDisplayName) { | ||
| return diag.Errorf("error updating BYOK Key %q: only %q attribute can be updated for BYOK Key", d.Id(), paramDisplayName) | ||
| } | ||
|
|
||
| tflog.Debug(ctx, fmt.Sprintf("Updating BYOK Key %q", d.Id()), map[string]interface{}{byokKeyLoggingKey: d.Id()}) | ||
|
|
||
| c := meta.(*Client) | ||
| updateByokKeyRequest := byok.NewByokV1KeyUpdate() | ||
|
|
||
| displayName := d.Get(paramDisplayName).(string) | ||
| updateByokKeyRequest.SetDisplayName(displayName) | ||
|
|
||
| updateByokKeyRequestJson, err := json.Marshal(updateByokKeyRequest) | ||
| if err != nil { | ||
| return diag.Errorf("error updating BYOK Key: error marshaling %#v to json: %s", updateByokKeyRequest, createDescriptiveError(err)) | ||
| } | ||
| tflog.Debug(ctx, fmt.Sprintf("Updating BYOK Key %q: %s", d.Id(), updateByokKeyRequestJson)) | ||
|
|
||
| _, _, err = executeKeyUpdate(ctx, c, d.Id(), *updateByokKeyRequest) | ||
| if err != nil { | ||
| return diag.Errorf("error updating BYOK Key %q: %s", d.Id(), createDescriptiveError(err)) | ||
| } | ||
|
|
||
| tflog.Debug(ctx, fmt.Sprintf("Finished updating BYOK Key %q", d.Id()), map[string]interface{}{byokKeyLoggingKey: d.Id()}) | ||
|
|
||
| return byokRead(ctx, d, meta) | ||
| } | ||
|
|
||
| func executeKeyUpdate(ctx context.Context, c *Client, id string, keyUpdate byok.ByokV1KeyUpdate) (byok.ByokV1Key, *http.Response, error) { | ||
| req := c.byokClient.KeysByokV1Api.UpdateByokV1Key(c.byokApiContext(ctx), id).ByokV1KeyUpdate(keyUpdate) | ||
| return req.Execute() | ||
| } | ||
|
|
||
| func byokImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
| tflog.Debug(ctx, fmt.Sprintf("Importing BYOK Key %q", d.Id()), map[string]interface{}{byokKeyLoggingKey: d.Id()}) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to have both computed and optional here? What's the default value if user doesn't provide display_name in the request data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backend does not compute a default value for this. It's fully optional.