From fd7a22c8ab0fe1b56da417d1459e4b467516a66a Mon Sep 17 00:00:00 2001 From: Anil Kumar Nagaraj Date: Thu, 15 Apr 2021 18:36:52 +0530 Subject: [PATCH] add wait logic for satellite location creation --- ibm/data_source_ibm_satellite_location.go | 6 ++ ibm/resource_ibm_satellite_location.go | 82 ++++++++++++++++--- ibm/resource_ibm_satellite_location_test.go | 13 ++- .../docs/d/satellite_location.html.markdown | 1 + .../docs/r/satellite_location.html.markdown | 12 ++- 5 files changed, 97 insertions(+), 17 deletions(-) diff --git a/ibm/data_source_ibm_satellite_location.go b/ibm/data_source_ibm_satellite_location.go index b006dc7281..aaa840e3a3 100644 --- a/ibm/data_source_ibm_satellite_location.go +++ b/ibm/data_source_ibm_satellite_location.go @@ -47,6 +47,11 @@ func dataSourceIBMSatelliteLocation() *schema.Resource { Computed: true, Description: "Location CRN", }, + "resource_group_id": { + Type: schema.TypeString, + Computed: true, + Description: "ID of the resource group", + }, }, } } @@ -75,6 +80,7 @@ func dataSourceIBMSatelliteLocationRead(d *schema.ResourceData, meta interface{} d.Set("zones", instance.WorkerZones) d.Set("managed_from", *instance.Datacenter) d.Set("crn", *instance.Crn) + d.Set("resource_group_id", *instance.ResourceGroup) return nil } diff --git a/ibm/resource_ibm_satellite_location.go b/ibm/resource_ibm_satellite_location.go index da43693d37..38f3e20748 100644 --- a/ibm/resource_ibm_satellite_location.go +++ b/ibm/resource_ibm_satellite_location.go @@ -19,8 +19,11 @@ const ( satLocation = "location" sateLocZone = "managed_from" - isLocationDeleting = "deleting" - isLocationDeleteDone = "done" + isLocationDeleting = "deleting" + isLocationDeleteDone = "done" + isLocationDeploying = "deploying" + isLocationReady = "action required" + isLocationDeployFailed = "deploy_failed" ) func resourceIBMSatelliteLocation() *schema.Resource { @@ -38,7 +41,7 @@ func resourceIBMSatelliteLocation() *schema.Resource { ), Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(10 * time.Minute), + Create: schema.DefaultTimeout(30 * time.Minute), Update: schema.DefaultTimeout(10 * time.Minute), Delete: schema.DefaultTimeout(60 * time.Minute), }, @@ -114,6 +117,12 @@ func resourceIBMSatelliteLocation() *schema.Resource { Set: schema.HashString, Description: "The names of at least three high availability zones to use for the location", }, + "resource_group_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: "ID of the resource group.", + }, }, } } @@ -138,28 +147,42 @@ func resourceIBMSatelliteLocationCreate(d *schema.ResourceData, meta interface{} createSatLocOptions.CosCredentials = expandCosCredentials(v.([]interface{})) } - if _, ok := d.GetOk("logging_account_id"); ok { - logAccID := d.Get("logging_account_id").(string) + if v, ok := d.GetOk("logging_account_id"); ok { + logAccID := v.(string) createSatLocOptions.LoggingAccountID = &logAccID } - if _, ok := d.GetOk("description"); ok { - desc := d.Get("description").(string) + if v, ok := d.GetOk("description"); ok { + desc := v.(string) createSatLocOptions.Description = &desc } - if _, ok := d.GetOk("zones"); ok { - z := d.Get("zones").(*schema.Set) + if v, ok := d.GetOk("zones"); ok { + z := v.(*schema.Set) createSatLocOptions.Zones = flatterSatelliteZones(z) } + if v, ok := d.GetOk("resource_group_id"); ok { + pathParamsMap := map[string]string{ + "X-Auth-Resource-Group": v.(string), + } + createSatLocOptions.Headers = pathParamsMap + } + instance, response, err := satClient.CreateSatelliteLocation(createSatLocOptions) if err != nil { return fmt.Errorf("Error Creating Satellite Location: %s\n%s", err, response) } d.SetId(satLocation) - log.Printf("[INFO] Created satellite location : %s", *instance.ID) + log.Printf("[INFO] Created satellite location : %s", satLocation) + + //Wait for location to be in ready state + _, err = waitForLocationToReady(*instance.ID, d, meta) + if err != nil { + return fmt.Errorf( + "Error waiting for location (%s) to reach ready state: %s", *instance.ID, err) + } return resourceIBMSatelliteLocationRead(d, meta) } @@ -197,6 +220,10 @@ func resourceIBMSatelliteLocationRead(d *schema.ResourceData, meta interface{}) d.Set("zones", instance.WorkerZones) } + if instance.ResourceGroup != nil { + d.Set("resource_group_id", instance.ResourceGroup) + } + return nil } @@ -267,3 +294,38 @@ func waitForLocationDelete(location string, d *schema.ResourceData, meta interfa return stateConf.WaitForState() } + +func waitForLocationToReady(loc string, d *schema.ResourceData, meta interface{}) (interface{}, error) { + satClient, err := meta.(ClientSession).SatelliteClientSession() + if err != nil { + return false, err + } + + stateConf := &resource.StateChangeConf{ + Pending: []string{isLocationDeploying}, + Target: []string{isLocationReady, isLocationDeployFailed}, + Refresh: func() (interface{}, string, error) { + getSatLocOptions := &kubernetesserviceapiv1.GetSatelliteLocationOptions{ + Controller: ptrToString(loc), + } + location, response, err := satClient.GetSatelliteLocation(getSatLocOptions) + if err != nil { + return nil, "", fmt.Errorf("Error Getting location : %s\n%s", err, response) + } + + if location != nil && *location.State == isLocationDeployFailed { + return location, isLocationDeployFailed, fmt.Errorf("The location is in failed state: %s", d.Id()) + } + + if location != nil && *location.State == isLocationReady { + return location, isLocationReady, nil + } + return location, isLocationDeploying, nil + }, + Timeout: d.Timeout(schema.TimeoutCreate), + Delay: 60 * time.Second, + MinTimeout: 60 * time.Second, + } + + return stateConf.WaitForState() +} diff --git a/ibm/resource_ibm_satellite_location_test.go b/ibm/resource_ibm_satellite_location_test.go index a43c3b8eb7..ebad2e11fd 100644 --- a/ibm/resource_ibm_satellite_location_test.go +++ b/ibm/resource_ibm_satellite_location_test.go @@ -123,11 +123,16 @@ func testAccCheckSatelliteLocationDestroy(s *terraform.State) error { func testAccCheckSatelliteLocationCreate(name, managed_from string) string { return fmt.Sprintf(` + data "ibm_resource_group" "res_group" { + is_default = true + } + resource "ibm_satellite_location" "location" { - location = "%s" - managed_from = "%s" - description = "test" - zones = ["us-east-1", "us-east-2", "us-east-3"] + location = "%s" + managed_from = "%s" + description = "test" + zones = ["us-east-1", "us-east-2", "us-east-3"] + resource_group_id = data.ibm_resource_group.res_group.id } `, name, managed_from) diff --git a/website/docs/d/satellite_location.html.markdown b/website/docs/d/satellite_location.html.markdown index 367951bdb4..247b5e3ad4 100644 --- a/website/docs/d/satellite_location.html.markdown +++ b/website/docs/d/satellite_location.html.markdown @@ -35,4 +35,5 @@ In addition to all arguments above, the following attributes are exported: * `description` - Description of the new Satellite location. * `logging_account_id` - The account ID for IBM Log Analysis with LogDNA log forwarding. * `zones` - The names for the host zones. For high availability, allocate your hosts across these three zones based on your infrastructure provider zones. ex: [ us-east-1, us-east-2, us-east-3 ] +* `resource_group_id` - The ID of the resource group. diff --git a/website/docs/r/satellite_location.html.markdown b/website/docs/r/satellite_location.html.markdown index 0b4c35da25..0b7db75a83 100644 --- a/website/docs/r/satellite_location.html.markdown +++ b/website/docs/r/satellite_location.html.markdown @@ -16,10 +16,15 @@ Create, update, or delete [IBM Cloud Satellite Location](https://cloud.ibm.com/d ### Create location ```hcl +data "ibm_resource_group" "group" { + name = "Default" +} + resource "ibm_satellite_location" "create_location" { - location = var.location - zones = var.location_zones - managed_from = var.managed_from + location = var.location + zones = var.location_zones + managed_from = var.managed_from + resource_group_id = data.ibm_resource_group.group.id } ``` @@ -56,6 +61,7 @@ The following arguments are supported: * `access_key-id` - The HMAC secret access key ID. * `secret_access_key` - The HMAC secret access key. * `zones` - (Optional, array of strings) The names for the host zones. For high availability, allocate your hosts across these three zones based on your infrastructure provider zones. ex: [ us-east-1, us-east-2, us-east-3 ] +* `resource_group_id` - (Optional, string) The ID of the resource group. You can retrieve the value from data source `ibm_resource_group`. ## Attributes Reference