Skip to content

Commit

Permalink
add wait logic for satellite location creation
Browse files Browse the repository at this point in the history
  • Loading branch information
anilkumarnagaraj authored and hkantare committed Apr 19, 2021
1 parent 6df8c25 commit fd7a22c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
6 changes: 6 additions & 0 deletions ibm/data_source_ibm_satellite_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
}
Expand Down Expand Up @@ -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
}
82 changes: 72 additions & 10 deletions ibm/resource_ibm_satellite_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
},
Expand Down Expand Up @@ -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.",
},
},
}
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
13 changes: 9 additions & 4 deletions ibm/resource_ibm_satellite_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/satellite_location.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

12 changes: 9 additions & 3 deletions website/docs/r/satellite_location.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit fd7a22c

Please sign in to comment.