Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vhost, user and exchange datasources #37

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions rabbitmq/data_source_exchange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package rabbitmq

import (
"context"
"fmt"
"log"

rabbithole "github.com/michaelklishin/rabbit-hole/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourcesExchange() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcesReadExchange,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"vhost": {
Type: schema.TypeString,
Optional: true,
Default: "/",
},
"settings": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},

"durable": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"auto_delete": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"arguments": {
Type: schema.TypeMap,
Optional: true,
},
},
},
},
},
}
}

func dataSourcesReadExchange(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

rmqc := meta.(*rabbithole.Client)

name := d.Get("name").(string)
vhost := d.Get("vhost").(string)
id := fmt.Sprintf("%s@%s", name, vhost)

exchangeSettings, err := rmqc.GetExchange(vhost, name)
if err != nil {
return diag.FromErr(checkDeleted(d, err))
}

log.Printf("[DEBUG] RabbitMQ: Exchange retrieved %s: %#v", id, exchangeSettings)

d.Set("name", exchangeSettings.Name)
d.Set("vhost", exchangeSettings.Vhost)

exchange := make([]map[string]interface{}, 1)
e := make(map[string]interface{})
e["type"] = exchangeSettings.Type
e["durable"] = exchangeSettings.Durable
e["auto_delete"] = exchangeSettings.AutoDelete
e["arguments"] = exchangeSettings.Arguments
exchange[0] = e
d.Set("settings", exchange)

d.SetId(id)

return diags
}
65 changes: 65 additions & 0 deletions rabbitmq/data_source_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package rabbitmq

import (
"context"
"log"

rabbithole "github.com/michaelklishin/rabbit-hole/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourcesUser() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcesReadUser,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}

func dataSourcesReadUser(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

rmqc := meta.(*rabbithole.Client)

name := d.Get("name").(string)

user, err := rmqc.GetUser(name)
if err != nil {
return diag.FromErr(checkDeleted(d, err))
}

log.Printf("[DEBUG] RabbitMQ: User retrieved: %#v", user)

d.Set("name", user.Name)

if len(user.Tags) > 0 {
var tagList []string
for _, v := range user.Tags {
if v != "" {
tagList = append(tagList, v)
}
}
if len(tagList) > 0 {
d.Set("tags", tagList)
}
}

d.SetId(name)

return diags
}
48 changes: 48 additions & 0 deletions rabbitmq/data_source_vhost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package rabbitmq

import (
"context"
"log"

rabbithole "github.com/michaelklishin/rabbit-hole/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourcesVhost() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcesReadVhost,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourcesReadVhost(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

rmqc := meta.(*rabbithole.Client)

name := d.Get("name").(string)

vhost, err := rmqc.GetVhost(name)
if err != nil {
return diag.FromErr(checkDeleted(d, err))
}

log.Printf("[DEBUG] RabbitMQ: Vhost retrieved: %#v", vhost)

d.Set("name", vhost.Name)

d.SetId(name)

return diags
}
13 changes: 13 additions & 0 deletions rabbitmq/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ func Provider() *schema.Provider {
"rabbitmq_vhost": resourceVhost(),
"rabbitmq_shovel": resourceShovel(),
},
DataSourcesMap: map[string]*schema.Resource{
// "rabbitmq_binding": dataSourcesBinding(),
"rabbitmq_exchange": dataSourcesExchange(),
// "rabbitmq_permissions": dataSourcesPermissions(),
// "rabbitmq_topic_permissions": dataSourcesTopicPermissions(),
// "rabbitmq_federation_upstream": dataSourcesFederationUpstream(),
// "rabbitmq_operator_policy": dataSourcesOperatorPolicy(),
// "rabbitmq_policy": dataSourcesPolicy(),
// "rabbitmq_queue": dataSourcesQueue(),
"rabbitmq_user": dataSourcesUser(),
"rabbitmq_vhost": dataSourcesVhost(),
// "rabbitmq_shovel": dataSourcesShovel(),
},

ConfigureFunc: providerConfigure,
}
Expand Down