Skip to content

Commit

Permalink
Merge branch 'main' into samples-pr-and-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tbroden84 authored Dec 13, 2023
2 parents 1a28af0 + da79e38 commit 2c4ec1e
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
with:
# Allow goreleaser to access older tag information.
fetch-depth: 0
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version-file: .go-version
- name: Build for single target with GoReleaser
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
with:
# Allow goreleaser to access older tag information.
fetch-depth: 0
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version-file: .go-version
- name: Import GPG key
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURES:

* Add resource that invoke account actions. ([#231](https://github.com/cloudamqp/terraform-provider-cloudamqp/pull/231))
* Add configurable retries for plugin resources ([#241](https://github.com/cloudamqp/terraform-provider-cloudamqp/pull/241))

IMPROVEMENTS:

Expand Down
26 changes: 22 additions & 4 deletions cloudamqp/data_source_cloudamqp_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func dataSourcePlugins() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep time in seconds between retries for plugins",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time in seconds for plugins",
},
},
},
},
Expand All @@ -47,10 +59,16 @@ func dataSourcePlugins() *schema.Resource {
}

func dataSourcePluginsRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
log.Printf("[DEBUG] cloudamqp::data_source::plugins::read instance id: %v", d.Get("instance_id"))
data, err := api.ReadPlugins(d.Get("instance_id").(int))
d.SetId(fmt.Sprintf("%v.plugins", d.Get("instance_id").(int)))
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

log.Printf("[DEBUG] cloudamqp::data_source::plugins::read instance id: %v", instanceID)
data, err := api.ListPlugins(instanceID, sleep, timeout)
d.SetId(fmt.Sprintf("%v.plugins", instanceID))
if err != nil {
return err
}
Expand Down
26 changes: 22 additions & 4 deletions cloudamqp/data_source_cloudamqp_plugins_community.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func dataSourcePluginsCommunity() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep time in seconds between retries for plugins",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time in seconds for plugins",
},
},
},
},
Expand All @@ -43,10 +55,16 @@ func dataSourcePluginsCommunity() *schema.Resource {
}

func dataSourcePluginsCommunityRead(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
log.Printf("[DEBUG] cloudamqp::data_source::plugin_comminity::read instance id: %v", d.Get("instance_id"))
data, err := api.ReadPluginsCommunity(d.Get("instance_id").(int))
d.SetId(fmt.Sprintf("%v.plugins_community", d.Get("instance_id").(int)))
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

log.Printf("[DEBUG] cloudamqp::data_source::plugin_comminity::read instance id: %v", instanceID)
data, err := api.ListPluginsCommunity(instanceID, sleep, timeout)
d.SetId(fmt.Sprintf("%v.plugins_community", instanceID))
if err != nil {
return err
}
Expand Down
83 changes: 63 additions & 20 deletions cloudamqp/resource_cloudamqp_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,70 @@ func resourcePlugin() *schema.Resource {
Computed: true,
Description: "The version of the plugin",
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep time in seconds between retries for plugins",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time in seconds for plugins",
},
},
}
}

func resourcePluginCreate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
_, err := api.EnablePlugin(d.Get("instance_id").(int), d.Get("name").(string))
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
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
}
d.SetId(d.Get("name").(string))
d.SetId(name)
return resourcePluginRead(d, meta)
}

func resourcePluginRead(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

// 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])
instanceID, _ := strconv.Atoi(s[1])
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 d.Get("instance_id").(int) == 0 {
if instanceID == 0 {
return errors.New("missing instance identifier: {resource_id},{instance_id}")
}

api := meta.(*api.API)
data, err := api.ReadPlugin(d.Get("instance_id").(int), d.Get("name").(string))
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 @@ -91,30 +127,37 @@ func resourcePluginRead(d *schema.ResourceData, meta interface{}) error {
}

func resourcePluginUpdate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
keys := []string{"name", "enabled"}
params := make(map[string]interface{})
for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}
var (
api = meta.(*api.API)
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)
)

_, err := api.UpdatePlugin(d.Get("instance_id").(int), params)
_, err := api.UpdatePlugin(instanceID, name, enabled, sleep, timeout)
if err != nil {
return fmt.Errorf("[Failed to update pluign: %v", err)
}
return resourcePluginRead(d, meta)
}

func resourcePluginDelete(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

if enableFasterInstanceDestroy {
log.Printf("[DEBUG] cloudamqp::resource::plugin::delete skip calling backend.")
return nil
}

api := meta.(*api.API)
return api.DeletePlugin(d.Get("instance_id").(int), d.Get("name").(string))
return api.DeletePlugin(instanceID, name, sleep, timeout)
}

func validatePluginSchemaAttribute(key string) bool {
Expand Down
81 changes: 60 additions & 21 deletions cloudamqp/resource_cloudamqp_plugin_community.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,41 @@ func resourcePluginCommunity() *schema.Resource {
Computed: true,
Description: "Required version of RabbitMQ",
},
"sleep": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Configurable sleep time in seconds between retries for plugins",
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 1800,
Description: "Configurable timeout time in seconds for plugins",
},
},
}
}

func resourcePluginCommunityCreate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
data, err := api.ReadPluginCommunity(d.Get("instance_id").(int), d.Get("name").(string))
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

data, err := api.ReadPluginCommunity(instanceID, name, sleep, timeout)
if err != nil {
return err
}

_, err = api.EnablePluginCommunity(d.Get("instance_id").(int), d.Get("name").(string))
_, err = api.InstallPluginCommunity(instanceID, name, sleep, timeout)
if err != nil {
return err
}
d.SetId(d.Get("name").(string))
d.SetId(name)

for k, v := range data {
if validateCommunityPluginSchemaAttribute(k) {
Expand All @@ -75,19 +94,31 @@ func resourcePluginCommunityCreate(d *schema.ResourceData, meta interface{}) err
}

func resourcePluginCommunityRead(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

// Support for importing resource
if strings.Contains(d.Id(), ",") {
s := strings.Split(d.Id(), ",")
d.SetId(s[0])
d.Set("name", s[0])
instanceID, _ := strconv.Atoi(s[1])
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 d.Get("instance_id").(int) == 0 {
if instanceID == 0 {
return errors.New("missing instance identifier: {resource_id},{instance_id}")
}

api := meta.(*api.API)
data, err := api.ReadPluginCommunity(d.Get("instance_id").(int), d.Get("name").(string))
data, err := api.ReadPluginCommunity(instanceID, name, sleep, timeout)
if err != nil {
return err
}
Expand All @@ -104,29 +135,37 @@ func resourcePluginCommunityRead(d *schema.ResourceData, meta interface{}) error
}

func resourcePluginCommunityUpdate(d *schema.ResourceData, meta interface{}) error {
api := meta.(*api.API)
keys := []string{"name", "enabled"}
params := make(map[string]interface{})
for _, k := range keys {
if v := d.Get(k); v != nil {
params[k] = v
}
}
_, err := api.UpdatePluginCommunity(d.Get("instance_id").(int), params)
var (
api = meta.(*api.API)
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)
)

_, err := api.UpdatePluginCommunity(instanceID, name, enabled, sleep, timeout)
if err != nil {
return err
}
return resourcePluginCommunityRead(d, meta)
}

func resourcePluginCommunityDelete(d *schema.ResourceData, meta interface{}) error {
var (
api = meta.(*api.API)
instanceID = d.Get("instance_id").(int)
name = d.Get("name").(string)
sleep = d.Get("sleep").(int)
timeout = d.Get("timeout").(int)
)

if enableFasterInstanceDestroy {
log.Printf("[DEBUG] cloudamqp::resource::plugin-community::delete skip calling backend.")
return nil
}

api := meta.(*api.API)
_, err := api.DisablePluginCommunity(d.Get("instance_id").(int), d.Get("name").(string))
_, err := api.UninstallPluginCommunity(instanceID, name, sleep, timeout)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cloudamqp/resource_cloudamqp_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func testAccCheckPluginEnabled(instanceName, resourceName string) resource.TestC
instanceID, _ := strconv.Atoi(rs.Primary.ID)

api := testAccProvider.Meta().(*api.API)
data, err := api.ReadPlugin(instanceID, pluginName)
data, err := api.ReadPlugin(instanceID, pluginName, 10, 1800)
if err != nil {
return fmt.Errorf("Error fetching item with resource %s. %s", resourceName, err)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func testAccCheckPluginDisable(instanceName, resourceName string) resource.TestC
}
instanceID, _ := strconv.Atoi(rs.Primary.ID)

data, err := api.ReadPlugin(instanceID, pluginName)
data, err := api.ReadPlugin(instanceID, pluginName, 10, 1800)
if err != nil {
return fmt.Errorf("Failed to retrieve plugin %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/integration_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ The following arguments are supported:
* `project_id` - (Optional/Computed) The project identifier.
* `private_key` - (Optional/Computed) The private access key.
* `client_email` - (Optional/Computed) The client email registered for the integration service.
* `tags` - (Optional) Tags. e.g. env=prod, region=europe.
* `tags` - (Optional) Tags. e.g. `env=prod,region=europe`.
* `queue_allowlist` - (Optional) Allowlist queues using regular expression. Leave empty to include all queues.
* `vhost_allowlist` - (Optional) Allowlist vhost using regular expression. Leave empty to include all vhosts.
* `queue_whitelist` - **Deprecated** Use queue_allowlist instead
Expand Down
Loading

0 comments on commit 2c4ec1e

Please sign in to comment.