Skip to content
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

[Fix] snapshot configuration update with kms key #2772

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func TestResourceCSSSnapshotConfigurationV1_basic(t *testing.T) {
Config: testResourceCSSSnapshotConfigurationV1Basic(name, bucketBasePath),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "creation_policy.0.prefix", "snap"),
resource.TestCheckResourceAttr(resourceName, "creation_policy.0.period", "16:00 GMT+01:00"),
resource.TestCheckResourceAttr(resourceName, "configuration.0.base_path", bucketBasePath),
),
},
{
Config: testResourceCSSSnapshotConfigurationV1Updated(name, bucketBasePath),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "creation_policy.0.prefix", "snapshot"),
resource.TestCheckResourceAttr(resourceName, "creation_policy.0.period", "17:00 GMT+01:00"),
resource.TestCheckResourceAttr(resourceName, "creation_policy.0.keepday", "2"),
),
},
Expand Down Expand Up @@ -87,7 +89,7 @@ resource "opentelekomcloud_css_snapshot_configuration_v1" "config" {
}
creation_policy {
prefix = "snap"
period = "00:00 GMT+03:00"
period = "16:00 GMT+01:00"
keepday = 1
enable = true
delete_auto = true
Expand Down Expand Up @@ -115,7 +117,7 @@ resource "opentelekomcloud_css_snapshot_configuration_v1" "config" {
}
creation_policy {
prefix = "snapshot"
period = "00:00 GMT+03:00"
period = "17:00 GMT+01:00"
keepday = 2
enable = true
delete_auto = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

func ResourceCssSnapshotConfigurationV1() *schema.Resource {
return &schema.Resource{
CreateContext: createResourceCssSnapshotConfigurationV1,
ReadContext: readResourceCssSnapshotConfigurationV1,
UpdateContext: updateResourceCssSnapshotConfigurationV1,
DeleteContext: deleteResourceCssSnapshotConfigurationV1,
CreateContext: createCssSnapshotConfigurationV1,
ReadContext: readCssSnapshotConfigurationV1,
UpdateContext: updateCssSnapshotConfigurationV1,
DeleteContext: deleteCssSnapshotConfigurationV1,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Expand All @@ -39,10 +39,11 @@ func ResourceCssSnapshotConfigurationV1() *schema.Resource {
ConflictsWith: []string{"configuration", "creation_policy"},
},
"configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Type: schema.TypeList,
ConflictsWith: []string{"automatic"},
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bucket": {
Expand All @@ -63,7 +64,6 @@ func ResourceCssSnapshotConfigurationV1() *schema.Resource {
},
},
},
ConflictsWith: []string{"automatic"},
},
"creation_policy": {
Type: schema.TypeList,
Expand Down Expand Up @@ -100,16 +100,38 @@ func ResourceCssSnapshotConfigurationV1() *schema.Resource {
}
}

func createResourceCssSnapshotConfigurationV1(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func createCssSnapshotConfigurationV1(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CssV1Client(config.GetRegion(d))
if err != nil {
return fmterr.Errorf(clientError, err)
}

clusterID := d.Get("cluster_id").(string)
d.SetId(clusterID)
if err := updateSnapshotConfigurationResource(ctx, d, meta); err != nil {
return diag.FromErr(err)

if d.Get("automatic").(bool) {
if err := snapshots.Enable(client, d.Id()); err != nil {
return fmterr.Errorf("error using automatic config for snapshots: %w", err)
}
return nil
}
return readResourceCssSnapshotConfigurationV1(ctx, d, meta)

if d.Get("configuration.#") != 0 {
if err := updateSnapshotConfiguration(client, d); err != nil {
return diag.FromErr(err)
}
}

if d.Get("creation_policy.#") != 0 {
if err := updateSnapshotPolicy(client, d); err != nil {
return diag.FromErr(err)
}
}
return readCssSnapshotConfigurationV1(ctx, d, meta)
}

func readResourceCssSnapshotConfigurationV1(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func readCssSnapshotConfigurationV1(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CssV1Client(config.GetRegion(d))
if err != nil {
Expand Down Expand Up @@ -145,14 +167,34 @@ func readResourceCssSnapshotConfigurationV1(_ context.Context, d *schema.Resourc
return nil
}

func updateResourceCssSnapshotConfigurationV1(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := updateSnapshotConfigurationResource(ctx, d, meta); err != nil {
return diag.FromErr(err)
func updateCssSnapshotConfigurationV1(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CssV1Client(config.GetRegion(d))
if err != nil {
return fmterr.Errorf(clientError, err)
}

if d.Get("automatic").(bool) {
if err := snapshots.Enable(client, d.Id()); err != nil {
return fmterr.Errorf("error using automatic config for snapshots: %w", err)
}
return nil
}
if d.HasChange("configuration") {
if err := updateSnapshotConfiguration(client, d); err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("creation_policy") {
if err := updateSnapshotPolicy(client, d); err != nil {
return diag.FromErr(err)
}
}
return readResourceCssSnapshotConfigurationV1(ctx, d, meta)

return readCssSnapshotConfigurationV1(ctx, d, meta)
}

func deleteResourceCssSnapshotConfigurationV1(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func deleteCssSnapshotConfigurationV1(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CssV1Client(config.GetRegion(d))
if err != nil {
Expand Down Expand Up @@ -197,32 +239,3 @@ func updateSnapshotConfiguration(client *golangsdk.ServiceClient, d *schema.Reso
}
return nil
}

func updateSnapshotConfigurationResource(_ context.Context, d *schema.ResourceData, meta interface{}) error {
config := meta.(*cfg.Config)
client, err := config.CssV1Client(config.GetRegion(d))
if err != nil {
return fmt.Errorf(clientError, err)
}

if d.Get("automatic").(bool) {
if err := snapshots.Enable(client, d.Id()); err != nil {
return fmt.Errorf("error using automatic config for snapshots: %w", err)
}
return nil
}

if d.Get("configuration.#") != 0 {
if err := updateSnapshotConfiguration(client, d); err != nil {
return err
}
}

if d.Get("creation_policy.#") != 0 {
if err := updateSnapshotPolicy(client, d); err != nil {
return err
}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
**[CSS]** fixed ``creation_policy`` update when kms encryption enabled ``resource/opentelekomcloud_css_snapshot_configuration_v1``
(`#2772 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2772>`_)
Loading