diff --git a/opentelekomcloud/acceptance/css/resource_opentelekomcloud_css_snapshot_configuration_v1_test.go b/opentelekomcloud/acceptance/css/resource_opentelekomcloud_css_snapshot_configuration_v1_test.go index 135832829..5da5b1e38 100644 --- a/opentelekomcloud/acceptance/css/resource_opentelekomcloud_css_snapshot_configuration_v1_test.go +++ b/opentelekomcloud/acceptance/css/resource_opentelekomcloud_css_snapshot_configuration_v1_test.go @@ -30,6 +30,7 @@ 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), ), }, @@ -37,6 +38,7 @@ func TestResourceCSSSnapshotConfigurationV1_basic(t *testing.T) { 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"), ), }, @@ -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 @@ -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 diff --git a/opentelekomcloud/services/css/resource_opentelekomcloud_css_snapshot_configuration_v1.go b/opentelekomcloud/services/css/resource_opentelekomcloud_css_snapshot_configuration_v1.go index 3db7973bf..98708658e 100644 --- a/opentelekomcloud/services/css/resource_opentelekomcloud_css_snapshot_configuration_v1.go +++ b/opentelekomcloud/services/css/resource_opentelekomcloud_css_snapshot_configuration_v1.go @@ -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), @@ -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": { @@ -63,7 +64,6 @@ func ResourceCssSnapshotConfigurationV1() *schema.Resource { }, }, }, - ConflictsWith: []string{"automatic"}, }, "creation_policy": { Type: schema.TypeList, @@ -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 { @@ -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 { @@ -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 -}