Skip to content

Commit

Permalink
[Fix] snapshot configuration update with kms key
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-sidelnikov committed Dec 19, 2024
1 parent 8054ca6 commit 9267eba
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 50 deletions.
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
}

0 comments on commit 9267eba

Please sign in to comment.