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

tech-debt: refactor mq data and resources to use different Read methods #18848

Merged
merged 2 commits into from
Apr 14, 2021
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
127 changes: 100 additions & 27 deletions aws/data_source_aws_mq_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mq"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsMqBroker() *schema.Resource {
Expand Down Expand Up @@ -250,42 +251,114 @@ func dataSourceAwsMqBroker() *schema.Resource {
}

func dataSourceAwsmQBrokerRead(d *schema.ResourceData, meta interface{}) error {
if brokerId, ok := d.GetOk("broker_id"); ok {
d.SetId(brokerId.(string))
} else {
conn := meta.(*AWSClient).mqconn
brokerName := d.Get("broker_name").(string)
conn := meta.(*AWSClient).mqconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &mq.ListBrokersInput{}
input := &mq.ListBrokersInput{}

err := conn.ListBrokersPages(input, func(page *mq.ListBrokersResponse, lastPage bool) bool {
if page == nil {
return !lastPage
}
var results []*mq.BrokerSummary

for _, brokerSummary := range page.BrokerSummaries {
if brokerSummary == nil {
continue
}
err := conn.ListBrokersPages(input, func(page *mq.ListBrokersResponse, lastPage bool) bool {
if page == nil {
return !lastPage
}

if aws.StringValue(brokerSummary.BrokerName) == brokerName {
d.Set("broker_id", brokerSummary.BrokerId)
d.SetId(aws.StringValue(brokerSummary.BrokerId))
return false
}
for _, brokerSummary := range page.BrokerSummaries {
if brokerSummary == nil {
continue
}

return !lastPage
})
if v, ok := d.GetOk("broker_id"); ok && v.(string) != aws.StringValue(brokerSummary.BrokerId) {
continue
}

if err != nil {
return fmt.Errorf("error listing MQ Brokers: %w", err)
}
if v, ok := d.GetOk("broker_name"); ok && v.(string) != aws.StringValue(brokerSummary.BrokerName) {
continue
}

if d.Id() == "" {
return fmt.Errorf("Failed to determine mq broker: %s", brokerName)
results = append(results, brokerSummary)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error listing MQ Brokers: %w", err)
}

if len(results) != 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(results))
}

brokerId := aws.StringValue(results[0].BrokerId)

output, err := conn.DescribeBroker(&mq.DescribeBrokerInput{
BrokerId: aws.String(brokerId),
})

if err != nil {
return fmt.Errorf("error reading MQ broker (%s): %w", brokerId, err)
}

if output == nil {
return fmt.Errorf("empty response while reading MQ broker (%s)", brokerId)
}

d.SetId(brokerId)

d.Set("arn", output.BrokerArn)
d.Set("authentication_strategy", output.AuthenticationStrategy)
d.Set("auto_minor_version_upgrade", output.AutoMinorVersionUpgrade)
d.Set("broker_id", brokerId)
d.Set("broker_name", output.BrokerName)
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
d.Set("deployment_mode", output.DeploymentMode)
d.Set("engine_type", output.EngineType)
d.Set("engine_version", output.EngineVersion)
d.Set("host_instance_type", output.HostInstanceType)
d.Set("instances", flattenMqBrokerInstances(output.BrokerInstances))
d.Set("publicly_accessible", output.PubliclyAccessible)
d.Set("security_groups", aws.StringValueSlice(output.SecurityGroups))
d.Set("storage_type", output.StorageType)
d.Set("subnet_ids", aws.StringValueSlice(output.SubnetIds))

if err := d.Set("configuration", flattenMqConfiguration(output.Configurations)); err != nil {
return fmt.Errorf("error setting configuration: %w", err)
}

if err := d.Set("encryption_options", flattenMqEncryptionOptions(output.EncryptionOptions)); err != nil {
return fmt.Errorf("error setting encryption_options: %w", err)
}

var password string
if v, ok := d.GetOk("ldap_server_metadata.0.service_account_password"); ok {
password = v.(string)
}

if err := d.Set("ldap_server_metadata", flattenMQLDAPServerMetadata(output.LdapServerMetadata, password)); err != nil {
return fmt.Errorf("error setting ldap_server_metadata: %w", err)
}

if err := d.Set("logs", flattenMqLogs(output.Logs)); err != nil {
return fmt.Errorf("error setting logs: %w", err)
}

if err := d.Set("maintenance_window_start_time", flattenMqWeeklyStartTime(output.MaintenanceWindowStartTime)); err != nil {
return fmt.Errorf("error setting maintenance_window_start_time: %w", err)
}

rawUsers, err := expandMqUsersForBroker(conn, brokerId, output.Users)

if err != nil {
return fmt.Errorf("error retrieving user info for MQ broker (%s): %w", brokerId, err)
}

if err := d.Set("user", flattenMqUsers(rawUsers, d.Get("user").(*schema.Set).List())); err != nil {
return fmt.Errorf("error setting user: %w", err)
}

if err := d.Set("tags", keyvaluetags.MqKeyValueTags(output.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return resourceAwsMqBrokerRead(d, meta)
return nil
}
118 changes: 58 additions & 60 deletions aws/resource_aws_mq_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ func resourceAwsMqBrokerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).mqconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

log.Printf("[INFO] Reading MQ Broker: %s", d.Id())
output, err := conn.DescribeBroker(&mq.DescribeBrokerInput{
BrokerId: aws.String(d.Id()),
})
Expand Down Expand Up @@ -423,70 +422,41 @@ func resourceAwsMqBrokerRead(d *schema.ResourceData, meta interface{}) error {
d.Set("storage_type", output.StorageType)
d.Set("subnet_ids", aws.StringValueSlice(output.SubnetIds))

if output.Configurations != nil && output.Configurations.Current != nil {
if err := d.Set("configuration", flattenMqConfigurationId(output.Configurations.Current)); err != nil {
return fmt.Errorf("error setting configuration: %w", err)
}
} else {
d.Set("configuration", nil)
if err := d.Set("configuration", flattenMqConfiguration(output.Configurations)); err != nil {
return fmt.Errorf("error setting configuration: %w", err)
}

if output.EncryptionOptions != nil {
if err := d.Set("encryption_options", flattenMqEncryptionOptions(output.EncryptionOptions)); err != nil {
return fmt.Errorf("error setting encryption_options: %w", err)
}
} else {
d.Set("encryption_options", nil)
if err := d.Set("encryption_options", flattenMqEncryptionOptions(output.EncryptionOptions)); err != nil {
return fmt.Errorf("error setting encryption_options: %w", err)
}

if output.LdapServerMetadata != nil {
password := ""
if v, ok := d.GetOk("ldap_server_metadata.0.service_account_password"); ok {
password = v.(string)
}
if err := d.Set("ldap_server_metadata", flattenMQLDAPServerMetadata(output.LdapServerMetadata, password)); err != nil {
return fmt.Errorf("error setting ldap_server_metadata: %w", err)
}
} else {
d.Set("ldap_server_metadata", nil)
var password string
if v, ok := d.GetOk("ldap_server_metadata.0.service_account_password"); ok {
password = v.(string)
}

if output.Logs != nil {
if err := d.Set("logs", flattenMqLogs(output.Logs)); err != nil {
return fmt.Errorf("error setting logs: %w", err)
}
} else {
d.Set("logs", nil)
if err := d.Set("ldap_server_metadata", flattenMQLDAPServerMetadata(output.LdapServerMetadata, password)); err != nil {
return fmt.Errorf("error setting ldap_server_metadata: %w", err)
}

if output.MaintenanceWindowStartTime != nil {
if err := d.Set("maintenance_window_start_time", flattenMqWeeklyStartTime(output.MaintenanceWindowStartTime)); err != nil {
return fmt.Errorf("error setting maintenance_window_start_time: %w", err)
}
} else {
d.Set("maintenance_window_start_time", nil)
if err := d.Set("logs", flattenMqLogs(output.Logs)); err != nil {
return fmt.Errorf("error setting logs: %w", err)
}

rawUsers := make([]*mq.User, len(output.Users))
for i, u := range output.Users {
uOut, err := conn.DescribeUser(&mq.DescribeUserInput{
BrokerId: aws.String(d.Id()),
Username: u.Username,
})
if err != nil {
return err
}
if err := d.Set("maintenance_window_start_time", flattenMqWeeklyStartTime(output.MaintenanceWindowStartTime)); err != nil {
return fmt.Errorf("error setting maintenance_window_start_time: %w", err)
}

rawUsers[i] = &mq.User{
ConsoleAccess: uOut.ConsoleAccess,
Groups: uOut.Groups,
Username: uOut.Username,
}
rawUsers, err := expandMqUsersForBroker(conn, d.Id(), output.Users)

if err != nil {
return fmt.Errorf("error retrieving user info for MQ broker (%s): %w", d.Id(), err)
}

if err := d.Set("user", flattenMqUsers(rawUsers, d.Get("user").(*schema.Set).List())); err != nil {
return fmt.Errorf("error setting user: %w", err)
}

if err := d.Set("tags", keyvaluetags.MqKeyValueTags(output.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}
Expand Down Expand Up @@ -788,6 +758,35 @@ func expandMqUsers(cfg []interface{}) []*mq.User {
return users
}

func expandMqUsersForBroker(conn *mq.MQ, brokerId string, input []*mq.UserSummary) ([]*mq.User, error) {
var rawUsers []*mq.User

for _, u := range input {
if u == nil {
continue
}

uOut, err := conn.DescribeUser(&mq.DescribeUserInput{
BrokerId: aws.String(brokerId),
Username: u.Username,
})

if err != nil {
return nil, err
}

user := &mq.User{
ConsoleAccess: uOut.ConsoleAccess,
Groups: uOut.Groups,
Username: uOut.Username,
}

rawUsers = append(rawUsers, user)
}

return rawUsers, nil
}

// We use cfgdUsers to get & set the password
func flattenMqUsers(users []*mq.User, cfgUsers []interface{}) *schema.Set {
existingPairs := make(map[string]string)
Expand All @@ -800,17 +799,17 @@ func flattenMqUsers(users []*mq.User, cfgUsers []interface{}) *schema.Set {
out := make([]interface{}, 0)
for _, u := range users {
m := map[string]interface{}{
"username": *u.Username,
"username": aws.StringValue(u.Username),
}
password := ""
if p, ok := existingPairs[*u.Username]; ok {
if p, ok := existingPairs[aws.StringValue(u.Username)]; ok {
password = p
}
if password != "" {
m["password"] = password
}
if u.ConsoleAccess != nil {
m["console_access"] = *u.ConsoleAccess
m["console_access"] = aws.BoolValue(u.ConsoleAccess)
}
if len(u.Groups) > 0 {
m["groups"] = flattenStringSet(u.Groups)
Expand Down Expand Up @@ -866,17 +865,16 @@ func expandMqConfigurationId(cfg []interface{}) *mq.ConfigurationId {
return &out
}

func flattenMqConfigurationId(cid *mq.ConfigurationId) []interface{} {
if cid == nil {
func flattenMqConfiguration(config *mq.Configurations) []interface{} {
if config == nil || config.Current == nil {
return []interface{}{}
}
m := make(map[string]interface{})
if cid.Id != nil {
m["id"] = *cid.Id
}
if cid.Revision != nil {
m["revision"] = *cid.Revision

m := map[string]interface{}{
"id": aws.StringValue(config.Current.Id),
"revision": aws.Int64Value(config.Current.Revision),
}

return []interface{}{m}
}

Expand Down