From b3933a00a58322ecefb5caaa03debaf63a6841db Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Tue, 13 Apr 2021 21:21:50 -0400 Subject: [PATCH 1/2] refactor mq data and resources to use different methods when reading state --- aws/data_source_aws_mq_broker.go | 126 ++++++++++++++++++++++++------- aws/resource_aws_mq_broker.go | 118 ++++++++++++++--------------- 2 files changed, 157 insertions(+), 87 deletions(-) diff --git a/aws/data_source_aws_mq_broker.go b/aws/data_source_aws_mq_broker.go index 5edfd915943..10f35495e5d 100644 --- a/aws/data_source_aws_mq_broker.go +++ b/aws/data_source_aws_mq_broker.go @@ -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 { @@ -250,42 +251,113 @@ 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_name", output.BrokerName) + 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 } diff --git a/aws/resource_aws_mq_broker.go b/aws/resource_aws_mq_broker.go index b43e1f9128b..e960a5ea35d 100644 --- a/aws/resource_aws_mq_broker.go +++ b/aws/resource_aws_mq_broker.go @@ -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()), }) @@ -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) } @@ -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) @@ -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) @@ -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} } From 834e77ec0d42f8ca6ef3a4d362144dfc41ceb6fd Mon Sep 17 00:00:00 2001 From: angie pinilla Date: Wed, 14 Apr 2021 11:17:37 -0400 Subject: [PATCH 2/2] call d.Set("broker_id",...) on Read Co-authored-by: Brian Flad --- aws/data_source_aws_mq_broker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/data_source_aws_mq_broker.go b/aws/data_source_aws_mq_broker.go index 10f35495e5d..7c9ad4d3a41 100644 --- a/aws/data_source_aws_mq_broker.go +++ b/aws/data_source_aws_mq_broker.go @@ -309,6 +309,7 @@ func dataSourceAwsmQBrokerRead(d *schema.ResourceData, meta interface{}) error { 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) d.Set("deployment_mode", output.DeploymentMode) d.Set("engine_type", output.EngineType)