Skip to content

Commit

Permalink
hashicorp#8000 hashicorp#8009 Optional fields should not be set unle…
Browse files Browse the repository at this point in the history
…ss it has a default or is explicitly populated.
  • Loading branch information
Lyle Nel committed Jan 27, 2020
1 parent a943020 commit 8ba2c1a
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 314 deletions.
120 changes: 66 additions & 54 deletions aws/resource_aws_dms_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ func resourceAwsDmsEndpoint() *schema.Resource {
},
"bucket_name": {
Type: schema.TypeString,
Optional: true,
Default: "",
Required: true,
},
"compression_type": {
Type: schema.TypeString,
Expand All @@ -217,27 +216,22 @@ func resourceAwsDmsEndpoint() *schema.Resource {
"timestamp_column_name": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"data_format": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"parquet_version": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"encryption_mode": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"server_side_encryption_kms_key_id": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
},
Expand All @@ -246,6 +240,15 @@ func resourceAwsDmsEndpoint() *schema.Resource {
}
}

func getStringRefOrNil(d *schema.ResourceData, key string) *string {
value, exists := d.GetOk(key)
if !exists {
return nil
}
valueString := value.(string)
return &valueString
}

func resourceAwsDmsEndpointCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dmsconn

Expand Down Expand Up @@ -287,18 +290,18 @@ func resourceAwsDmsEndpointCreate(d *schema.ResourceData, meta interface{}) erro
request.DatabaseName = aws.String(d.Get("database_name").(string))
case "s3":
request.S3Settings = &dms.S3Settings{
ServiceAccessRoleArn: aws.String(d.Get("s3_settings.0.service_access_role_arn").(string)),
ExternalTableDefinition: aws.String(d.Get("s3_settings.0.external_table_definition").(string)),
CsvRowDelimiter: aws.String(d.Get("s3_settings.0.csv_row_delimiter").(string)),
CsvDelimiter: aws.String(d.Get("s3_settings.0.csv_delimiter").(string)),
BucketFolder: aws.String(d.Get("s3_settings.0.bucket_folder").(string)),
BucketName: aws.String(d.Get("s3_settings.0.bucket_name").(string)),
CompressionType: aws.String(d.Get("s3_settings.0.compression_type").(string)),
TimestampColumnName: aws.String(d.Get("s3_settings.0.timestamp_column_name").(string)),
DataFormat: aws.String(d.Get("s3_settings.0.data_format").(string)),
ParquetVersion: aws.String(d.Get("s3_settings.0.parquet_version").(string)),
EncryptionMode: aws.String(d.Get("s3_settings.0.encryption_mode").(string)),
ServerSideEncryptionKmsKeyId: aws.String(d.Get("s3_settings.0.server_side_encryption_kms_key_id").(string)),
ServiceAccessRoleArn: getStringRefOrNil(d, "s3_settings.0.service_access_role_arn"),
ExternalTableDefinition: getStringRefOrNil(d, "s3_settings.0.external_table_definition"),
CsvRowDelimiter: getStringRefOrNil(d, "s3_settings.0.csv_row_delimiter"),
CsvDelimiter: getStringRefOrNil(d, "s3_settings.0.csv_delimiter"),
BucketFolder: getStringRefOrNil(d, "s3_settings.0.bucket_folder"),
BucketName: getStringRefOrNil(d, "s3_settings.0.bucket_name"),
CompressionType: getStringRefOrNil(d, "s3_settings.0.compression_type"),
TimestampColumnName: getStringRefOrNil(d, "s3_settings.0.timestamp_column_name"),
DataFormat: getStringRefOrNil(d, "s3_settings.0.data_format"),
ParquetVersion: getStringRefOrNil(d, "s3_settings.0.parquet_version"),
EncryptionMode: getStringRefOrNil(d, "s3_settings.0.encryption_mode"),
ServerSideEncryptionKmsKeyId: getStringRefOrNil(d, "s3_settings.0.server_side_encryption_kms_key_id"),
}
default:
request.Password = aws.String(d.Get("password").(string))
Expand Down Expand Up @@ -346,20 +349,27 @@ func resourceAwsDmsEndpointCreate(d *schema.ResourceData, meta interface{}) erro
}

d.SetId(d.Get("endpoint_id").(string))
response, _ := describeEndpointResponse(conn, d)
log.Println("[DEBUG] DMS response from create endpoint:", response)
return resourceAwsDmsEndpointRead(d, meta)
}

func resourceAwsDmsEndpointRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dmsconn

response, err := conn.DescribeEndpoints(&dms.DescribeEndpointsInput{
func describeEndpointResponse(conn *dms.DatabaseMigrationService, d *schema.ResourceData) (*dms.DescribeEndpointsOutput, error) {
response, err2 := conn.DescribeEndpoints(&dms.DescribeEndpointsInput{
Filters: []*dms.Filter{
{
Name: aws.String("endpoint-id"),
Values: []*string{aws.String(d.Id())}, // Must use d.Id() to work with import.
},
},
})
return response, err2
}

func resourceAwsDmsEndpointRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dmsconn

response, err := describeEndpointResponse(conn, d)
if err != nil {
if dmserr, ok := err.(awserr.Error); ok && dmserr.Code() == "ResourceNotFoundFault" {
log.Printf("[DEBUG] DMS Replication Endpoint %q Not Found", d.Id())
Expand Down Expand Up @@ -495,24 +505,23 @@ func resourceAwsDmsEndpointUpdate(d *schema.ResourceData, meta interface{}) erro
d.HasChange("s3_settings.0.bucket_folder") ||
d.HasChange("s3_settings.0.bucket_name") ||
d.HasChange("s3_settings.0.compression_type") ||
d.HasChange("s3_settings.0.timestamp_column_name") ||
d.HasChange("s3_settings.0.data_format") ||
d.HasChange("s3_settings.0.parquet_version") ||
d.HasChange("s3_settings.0.encryption_mode") ||
d.HasChange("s3_settings.0.server_side_encryption_kms_key_id") {
request.S3Settings = &dms.S3Settings{
ServiceAccessRoleArn: aws.String(d.Get("s3_settings.0.service_access_role_arn").(string)),
ExternalTableDefinition: aws.String(d.Get("s3_settings.0.external_table_definition").(string)),
CsvRowDelimiter: aws.String(d.Get("s3_settings.0.csv_row_delimiter").(string)),
CsvDelimiter: aws.String(d.Get("s3_settings.0.csv_delimiter").(string)),
BucketFolder: aws.String(d.Get("s3_settings.0.bucket_folder").(string)),
BucketName: aws.String(d.Get("s3_settings.0.bucket_name").(string)),
CompressionType: aws.String(d.Get("s3_settings.0.compression_type").(string)),
TimestampColumnName: aws.String(d.Get("s3_settings.0.timestamp_column_name").(string)),
DataFormat: aws.String(d.Get("s3_settings.0.data_format").(string)),
ParquetVersion: aws.String(d.Get("s3_settings.0.parquet_version").(string)),
EncryptionMode: aws.String(d.Get("s3_settings.0.encryption_mode").(string)),
ServerSideEncryptionKmsKeyId: aws.String(d.Get("s3_settings.0.server_side_encryption_kms_key_id").(string)),
ServiceAccessRoleArn: getStringRefOrNil(d, "s3_settings.0.service_access_role_arn"),
ExternalTableDefinition: getStringRefOrNil(d, "s3_settings.0.external_table_definition"),
CsvRowDelimiter: getStringRefOrNil(d, "s3_settings.0.csv_row_delimiter"),
CsvDelimiter: getStringRefOrNil(d, "s3_settings.0.csv_delimiter"),
BucketFolder: getStringRefOrNil(d, "s3_settings.0.bucket_folder"),
BucketName: getStringRefOrNil(d, "s3_settings.0.bucket_name"),
TimestampColumnName: getStringRefOrNil(d, "s3_settings.0.timestamp_column_name"),
CompressionType: getStringRefOrNil(d, "s3_settings.0.compression_type"),
DataFormat: getStringRefOrNil(d, "s3_settings.0.data_format"),
ParquetVersion: getStringRefOrNil(d, "s3_settings.0.parquet_version"),
EncryptionMode: getStringRefOrNil(d, "s3_settings.0.encryption_mode"),
ServerSideEncryptionKmsKeyId: getStringRefOrNil(d, "s3_settings.0.server_side_encryption_kms_key_id"),
}
request.EngineName = aws.String(d.Get("engine_name").(string)) // Must be included (should be 's3')
hasChanges = true
Expand Down Expand Up @@ -639,24 +648,27 @@ func flattenDmsMongoDbSettings(settings *dms.MongoDbSettings) []map[string]inter
}

func flattenDmsS3Settings(settings *dms.S3Settings) []map[string]interface{} {
if settings == nil {
return []map[string]interface{}{}
result := make([]map[string]interface{}, 0, 1)

mapWithNils := map[string]interface{}{
"service_access_role_arn": settings.ServiceAccessRoleArn,
"external_table_definition": settings.ExternalTableDefinition,
"csv_row_delimiter": settings.CsvRowDelimiter,
"csv_delimiter": settings.CsvDelimiter,
"bucket_folder": settings.BucketFolder,
"bucket_name": settings.BucketName,
"compression_type": settings.CompressionType,
"timestamp_column_name": settings.TimestampColumnName,
"data_format": settings.DataFormat,
"parquet_version": settings.ParquetVersion,
"encryption_mode": settings.EncryptionMode,
"server_side_encryption_kms_key_id": settings.ServerSideEncryptionKmsKeyId,
}

m := map[string]interface{}{
"service_access_role_arn": aws.StringValue(settings.ServiceAccessRoleArn),
"external_table_definition": aws.StringValue(settings.ExternalTableDefinition),
"csv_row_delimiter": aws.StringValue(settings.CsvRowDelimiter),
"csv_delimiter": aws.StringValue(settings.CsvDelimiter),
"bucket_folder": aws.StringValue(settings.BucketFolder),
"bucket_name": aws.StringValue(settings.BucketName),
"compression_type": aws.StringValue(settings.CompressionType),
"timestamp_column_name": aws.StringValue(settings.TimestampColumnName),
"data_format": aws.StringValue(settings.DataFormat),
"parquet_version": aws.StringValue(settings.ParquetVersion),
"encryption_mode": aws.StringValue(settings.EncryptionMode),
"server_side_encryption_kms_key_id": aws.StringValue(settings.ServerSideEncryptionKmsKeyId),
mapWithoutNills := make(map[string]interface{})
for key := range mapWithNils {
if mapWithNils[key].(*string) != nil {
mapWithoutNills[key] = aws.StringValue(mapWithNils[key].(*string))
}
}

return []map[string]interface{}{m}
return append(result, mapWithoutNills)
}
Loading

0 comments on commit 8ba2c1a

Please sign in to comment.