Skip to content

Commit

Permalink
Fix HTTP 415 when retrieving tags after nodes role switch (#564)
Browse files Browse the repository at this point in the history
Make provider use `master` node instead of node with ID `_node0` for getting tags
  • Loading branch information
outcatcher authored Aug 13, 2020
1 parent 9b893e7 commit a976c75
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
82 changes: 37 additions & 45 deletions opentelekomcloud/resource_opentelekomcloud_rds_instance_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func resourceRdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error
d.SetId(id.(string))

if hasFilledOpt(d, "tag") {
var node_id string
var nodeID string
res := make(map[string]interface{})
v, err := fetchRdsInstanceV3ByList(d, client)
if err != nil {
Expand All @@ -312,14 +312,8 @@ func resourceRdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error
return err
}

nodes := d.Get("nodes").([]interface{})
for _, node := range nodes {
name := node.(map[string]interface{})["name"].(string)
if strings.HasSuffix(name, "_node0") {
node_id = node.(map[string]interface{})["id"].(string)
}
}
if node_id == "" {
nodeID = getMasterID(d.Get("nodes").([]interface{}))
if nodeID == "" {
log.Printf("[WARN] Error setting tag(key/value) of instance:%s", id.(string))
return nil
}
Expand All @@ -334,7 +328,7 @@ func resourceRdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error
Key: key,
Value: val.(string),
}
err = tags.Create(tagClient, node_id, tagOpts).ExtractErr()
err = tags.Create(tagClient, nodeID, tagOpts).ExtractErr()
if err != nil {
log.Printf("[WARN] Error setting tag(key/value) of instance:%s, err=%s", id.(string), err)
}
Expand Down Expand Up @@ -369,7 +363,7 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
}

// Fetching node id
var node_id string
var nodeID string
res := make(map[string]interface{})
v, err := fetchRdsInstanceV3ByList(d, rdsClient)
if err != nil {
Expand All @@ -383,14 +377,8 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
return err
}

nodes := v.([]interface{})
for _, node := range nodes {
name := node.(map[string]interface{})["name"].(string)
if strings.HasSuffix(name, "_node0") {
node_id = node.(map[string]interface{})["id"].(string)
}
}
if node_id == "" {
nodeID = getMasterID(v.([]interface{}))
if nodeID == "" {
log.Printf("[WARN] Error fetching node id of instance:%s", d.Id())
return nil
}
Expand All @@ -407,15 +395,15 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error

if len(remove) > 0 {
for _, opts := range remove {
err = tags.Delete(tagClient, node_id, opts).ExtractErr()
err = tags.Delete(tagClient, nodeID, opts).ExtractErr()
if err != nil {
log.Printf("[WARN] Error deleting tag(key/value) of instance:%s, err=%s", d.Id(), err)
}
}
}
if len(create) > 0 {
for _, opts := range create {
err = tags.Create(tagClient, node_id, opts).ExtractErr()
err = tags.Create(tagClient, nodeID, opts).ExtractErr()
if err != nil {
log.Printf("[WARN] Error setting tag(key/value) of instance:%s, err=%s", d.Id(), err)
}
Expand All @@ -432,18 +420,18 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error

// Fetch flavor id
db := d.Get("db").([]interface{})
datastore_name := db[0].(map[string]interface{})["type"].(string)
datastore_version := db[0].(map[string]interface{})["version"].(string)
datastoresList, err := datastores.List(client, datastore_name).Extract()
datastoreName := db[0].(map[string]interface{})["type"].(string)
datastoreVersion := db[0].(map[string]interface{})["version"].(string)
datastoreList, err := datastores.List(client, datastoreName).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve datastores: %s ", err)
}
if len(datastoresList) < 1 {
if len(datastoreList) < 1 {
return fmt.Errorf("Returned no datastore result. ")
}
var datastoreId string
for _, datastore := range datastoresList {
if strings.HasPrefix(datastore.Name, datastore_version) {
for _, datastore := range datastoreList {
if strings.HasPrefix(datastore.Name, datastoreVersion) {
datastoreId = datastore.ID
break
}
Expand Down Expand Up @@ -472,15 +460,15 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Update flavor: %s", nflavor.(string))

updateFlavorOpts.FlavorRef = rdsFlavor.ID
_, err = instances.UpdateFlavorRef(client, updateFlavorOpts, node_id).Extract()
_, err = instances.UpdateFlavorRef(client, updateFlavorOpts, nodeID).Extract()
if err != nil {
return fmt.Errorf("Error updating instance Flavor from result: %s ", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"MODIFYING"},
Target: []string{"ACTIVE"},
Refresh: instanceStateFlavorUpdateRefreshFunc(client, node_id, d.Get("flavor").(string)),
Refresh: instanceStateFlavorUpdateRefreshFunc(client, nodeID, d.Get("flavor").(string)),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 15 * time.Second,
MinTimeout: 3 * time.Second,
Expand All @@ -490,9 +478,9 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) flavor to be Updated: %s ",
node_id, err)
nodeID, err)
}
log.Printf("[DEBUG] Successfully updated instance %s flavor: %s", node_id, d.Get("flavor").(string))
log.Printf("[DEBUG] Successfully updated instance %s flavor: %s", nodeID, d.Get("flavor").(string))
}

// Update volume
Expand All @@ -513,15 +501,15 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
}
log.Printf("[DEBUG] volume: %+v", volume)
updateOpts.Volume = volume
_, err = instances.UpdateVolumeSize(client, updateOpts, node_id).Extract()
_, err = instances.UpdateVolumeSize(client, updateOpts, nodeID).Extract()
if err != nil {
return fmt.Errorf("Error updating instance volume from result: %s ", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"MODIFYING"},
Target: []string{"UPDATED"},
Refresh: instanceStateUpdateRefreshFunc(client, node_id, updateOpts.Volume["size"].(int)),
Refresh: instanceStateUpdateRefreshFunc(client, nodeID, updateOpts.Volume["size"].(int)),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 15 * time.Second,
MinTimeout: 3 * time.Second,
Expand All @@ -531,14 +519,24 @@ func resourceRdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) volume to be Updated: %s ",
node_id, err)
nodeID, err)
}
log.Printf("[DEBUG] Successfully updated instance %s volume: %+v", node_id, volume)
log.Printf("[DEBUG] Successfully updated instance %s volume: %+v", nodeID, volume)
}

return resourceRdsInstanceV3Read(d, meta)
}

func getMasterID(nodes []interface{}) (nodeID string) {
for _, node := range nodes {
nodeObj := node.(map[string]interface{})
if nodeObj["role"].(string) == "master" {
nodeID = nodeObj["id"].(string)
}
}
return
}

func resourceRdsInstanceV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.sdkClient(GetRegion(d, config), "rdsv1", serviceProjectLevel)
Expand Down Expand Up @@ -567,27 +565,21 @@ func resourceRdsInstanceV3Read(d *schema.ResourceData, meta interface{}) error {
}

// set instance tag
var node_id string
var nodeID string
nodes := d.Get("nodes").([]interface{})
for _, node := range nodes {
name := node.(map[string]interface{})["name"].(string)
if strings.HasSuffix(name, "_node0") {
node_id = node.(map[string]interface{})["id"].(string)
}
}
if node_id == "" {
nodeID = getMasterID(nodes)
if nodeID == "" {
log.Printf("[WARN] Error fetching node id of instance:%s", d.Id())
return nil
}
tagClient, err := config.rdsTagV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud rds tag client: %#v", err)
}
taglist, err := tags.Get(tagClient, node_id).Extract()
taglist, err := tags.Get(tagClient, nodeID).Extract()
if err != nil {
return fmt.Errorf("Error fetching OpenTelekomCloud rds instance tags: %s", err)
}

tagmap := make(map[string]string)
for _, val := range taglist.Tags {
tagmap[val.Key] = val.Value
Expand Down
12 changes: 10 additions & 2 deletions opentelekomcloud/resource_opentelekomcloud_rds_instance_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func TestAccRdsInstanceV3_basic(t *testing.T) {

func testAccRdsInstanceV3_basic(val string) string {
return fmt.Sprintf(`
resource opentelekomcloud_networking_secgroup_v2 sg {
name = "sg-rds-test"
}
resource "opentelekomcloud_rds_instance_v3" "instance" {
availability_zone = ["%s"]
db {
Expand All @@ -59,7 +63,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
port = "8635"
}
name = "terraform_test_rds_instance%s"
security_group_id = "47f50ead-ce99-4b71-b814-d81dba416ac2"
security_group_id = opentelekomcloud_networking_secgroup_v2.sg.id
subnet_id = "%s"
vpc_id = "%s"
volume {
Expand All @@ -81,6 +85,10 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {

func testAccRdsInstanceV3_update(val string) string {
return fmt.Sprintf(`
resource opentelekomcloud_networking_secgroup_v2 sg {
name = "sg-rds-test"
}
resource "opentelekomcloud_rds_instance_v3" "instance" {
availability_zone = ["%s"]
db {
Expand All @@ -90,7 +98,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
port = "8635"
}
name = "terraform_test_rds_instance%s"
security_group_id = "47f50ead-ce99-4b71-b814-d81dba416ac2"
security_group_id = opentelekomcloud_networking_secgroup_v2.sg.id
subnet_id = "%s"
vpc_id = "%s"
volume {
Expand Down

0 comments on commit a976c75

Please sign in to comment.