Skip to content

Commit

Permalink
Remove params and set default values on import
Browse files Browse the repository at this point in the history
  • Loading branch information
tbroden84 committed Nov 24, 2023
1 parent c5ec470 commit 52ea49b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
25 changes: 14 additions & 11 deletions cloudamqp/resource_cloudamqp_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func resourcePluginCreate(d *schema.ResourceData, meta interface{}) error {
timeout = d.Get("timeout").(int)
)

log.Printf("[DEBUG] create plugin instanceID: %v, name: %v, sleep: %v, timeout: %v",
instanceID, name, sleep, timeout)
_, err := api.EnablePlugin(instanceID, name, sleep, timeout)
if err != nil {
return err
Expand All @@ -91,16 +93,23 @@ func resourcePluginRead(d *schema.ResourceData, meta interface{}) error {

// Support for importing resource
if strings.Contains(d.Id(), ",") {
log.Printf("[DEBUG] import plugin instanceID: %v, id: %v", instanceID, d.Id())
s := strings.Split(d.Id(), ",")
d.SetId(s[0])
d.Set("name", s[0])
name = s[0]
d.SetId(name)
d.Set("name", name)
instanceID, _ = strconv.Atoi(s[1])
d.Set("instance_id", instanceID)
// Set default values for optional arguments
d.Set("sleep", 10)
d.Set("timeout", 1800)
}
if instanceID == 0 {
return errors.New("missing instance identifier: {resource_id},{instance_id}")
}

log.Printf("[DEBUG] import plugin instanceID: %v, name: %v, sleep: %v, timeout: %v",
instanceID, name, sleep, timeout)
data, err := api.ReadPlugin(instanceID, name, sleep, timeout)
if err != nil {
return err
Expand All @@ -120,20 +129,14 @@ func resourcePluginRead(d *schema.ResourceData, meta interface{}) error {
func resourcePluginUpdate(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
keys = []string{"name", "enabled"}
params map[string]interface{}
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
enabled = d.Get("enabled").(bool)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}

_, err := api.UpdatePlugin(instanceID, params, sleep, timeout)
_, err := api.UpdatePlugin(instanceID, name, enabled, sleep, timeout)
if err != nil {
return fmt.Errorf("[Failed to update pluign: %v", err)
}
Expand Down
19 changes: 9 additions & 10 deletions cloudamqp/resource_cloudamqp_plugin_community.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ func resourcePluginCommunityRead(d *schema.ResourceData, meta interface{}) error
// Support for importing resource
if strings.Contains(d.Id(), ",") {
s := strings.Split(d.Id(), ",")
d.SetId(s[0])
d.Set("name", s[0])
name = s[0]
d.SetId(name)
d.Set("name", name)
instanceID, _ = strconv.Atoi(s[1])
d.Set("instance_id", instanceID)
// Set default values for optional arguments
d.Set("sleep", 10)
d.Set("timeout", 1800)
}
if instanceID == 0 {
return errors.New("missing instance identifier: {resource_id},{instance_id}")
Expand All @@ -133,19 +137,14 @@ func resourcePluginCommunityRead(d *schema.ResourceData, meta interface{}) error
func resourcePluginCommunityUpdate(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
keys = []string{"name", "enabled"}
params map[string]interface{}
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
enabled = d.Get("enabled").(bool)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}
_, err := api.UpdatePluginCommunity(instanceID, params, sleep, timeout)
_, err := api.UpdatePluginCommunity(instanceID, name, enabled, sleep, timeout)
if err != nil {
return err
}
Expand Down

0 comments on commit 52ea49b

Please sign in to comment.