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 AWS Config Conformance Pack resource #17313

Merged
merged 8 commits into from
Feb 11, 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
3 changes: 3 additions & 0 deletions .changelog/17313.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_config_conformance_pack
```
124 changes: 124 additions & 0 deletions aws/configservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,78 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
ConfigConformancePackCreateTimeout = 5 * time.Minute
ConfigConformancePackDeleteTimeout = 5 * time.Minute

ConfigConformancePackStatusNotFound = "NotFound"
ConfigConformancePackStatusUnknown = "Unknown"
)

func configDescribeConformancePack(conn *configservice.ConfigService, name string) (*configservice.ConformancePackDetail, error) {
input := &configservice.DescribeConformancePacksInput{
ConformancePackNames: []*string{aws.String(name)},
}

for {
output, err := conn.DescribeConformancePacks(input)

if err != nil {
return nil, err
}

for _, pack := range output.ConformancePackDetails {
if pack == nil {
continue
}

if aws.StringValue(pack.ConformancePackName) == name {
return pack, nil
}
}

if aws.StringValue(output.NextToken) == "" {
break
}

input.NextToken = output.NextToken
}

return nil, nil
}

func configDescribeConformancePackStatus(conn *configservice.ConfigService, name string) (*configservice.ConformancePackStatusDetail, error) {
input := &configservice.DescribeConformancePackStatusInput{
ConformancePackNames: []*string{aws.String(name)},
}

for {
output, err := conn.DescribeConformancePackStatus(input)

if err != nil {
return nil, err
}

for _, status := range output.ConformancePackStatusDetails {
if aws.StringValue(status.ConformancePackName) == name {
return status, nil
}
}

if aws.StringValue(output.NextToken) == "" {
break
}

input.NextToken = output.NextToken
}

return nil, nil
}

func configDescribeOrganizationConfigRule(conn *configservice.ConfigService, name string) (*configservice.OrganizationConfigRule, error) {
input := &configservice.DescribeOrganizationConfigRulesInput{
OrganizationConfigRuleNames: []*string{aws.String(name)},
Expand Down Expand Up @@ -94,6 +163,26 @@ func configGetOrganizationConfigRuleDetailedStatus(conn *configservice.ConfigSer
return statuses, nil
}

func configRefreshConformancePackStatus(conn *configservice.ConfigService, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
status, err := configDescribeConformancePackStatus(conn, name)

if err != nil {
return nil, ConfigConformancePackStatusUnknown, err
}

if status == nil {
return nil, ConfigConformancePackStatusNotFound, nil
}

if errMsg := aws.StringValue(status.ConformancePackStatusReason); errMsg != "" {
return status, aws.StringValue(status.ConformancePackState), fmt.Errorf(errMsg)
}

return status, aws.StringValue(status.ConformancePackState), nil
}
}

func configRefreshOrganizationConfigRuleStatus(conn *configservice.ConfigService, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
status, err := configDescribeOrganizationConfigRuleStatus(conn, name)
Expand Down Expand Up @@ -132,6 +221,41 @@ func configRefreshOrganizationConfigRuleStatus(conn *configservice.ConfigService
}
}

func configWaitForConformancePackStateCreateComplete(conn *configservice.ConfigService, name string) error {
stateChangeConf := resource.StateChangeConf{
Pending: []string{configservice.ConformancePackStateCreateInProgress},
Target: []string{configservice.ConformancePackStateCreateComplete},
Timeout: ConfigConformancePackCreateTimeout,
Refresh: configRefreshConformancePackStatus(conn, name),
}

_, err := stateChangeConf.WaitForState()

if tfawserr.ErrCodeEquals(err, configservice.ErrCodeNoSuchConformancePackException) {
return nil
}

return err

}

func configWaitForConformancePackStateDeleteComplete(conn *configservice.ConfigService, name string) error {
stateChangeConf := resource.StateChangeConf{
Pending: []string{configservice.ConformancePackStateDeleteInProgress},
Target: []string{},
Timeout: ConfigConformancePackDeleteTimeout,
Refresh: configRefreshConformancePackStatus(conn, name),
}

_, err := stateChangeConf.WaitForState()

if tfawserr.ErrCodeEquals(err, configservice.ErrCodeNoSuchConformancePackException) {
return nil
}

return err
}

func configWaitForOrganizationRuleStatusCreateSuccessful(conn *configservice.ConfigService, name string, timeout time.Duration) error {
stateChangeConf := &resource.StateChangeConf{
Pending: []string{configservice.OrganizationRuleStatusCreateInProgress},
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ func Provider() *schema.Provider {
"aws_config_configuration_aggregator": resourceAwsConfigConfigurationAggregator(),
"aws_config_configuration_recorder": resourceAwsConfigConfigurationRecorder(),
"aws_config_configuration_recorder_status": resourceAwsConfigConfigurationRecorderStatus(),
"aws_config_conformance_pack": resourceAwsConfigConformancePack(),
"aws_config_delivery_channel": resourceAwsConfigDeliveryChannel(),
"aws_config_organization_custom_rule": resourceAwsConfigOrganizationCustomRule(),
"aws_config_organization_managed_rule": resourceAwsConfigOrganizationManagedRule(),
Expand Down
Loading