Skip to content

Commit

Permalink
CR updates including documentation, acctest coverage, and new error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
anGie44 committed Feb 11, 2021
1 parent 26a9df7 commit 76758ea
Show file tree
Hide file tree
Showing 5 changed files with 755 additions and 305 deletions.
198 changes: 124 additions & 74 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 Expand Up @@ -177,77 +301,3 @@ func configWaitForOrganizationRuleStatusUpdateSuccessful(conn *configservice.Con

return err
}

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 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 expandConfigConformancePackParameters(m map[string]interface{}) (params []*configservice.ConformancePackInputParameter) {
for k, v := range m {
params = append(params, &configservice.ConformancePackInputParameter{
ParameterName: aws.String(k),
ParameterValue: aws.String(v.(string)),
})
}
return
}

func flattenConformancePackInputParameters(parameters []*configservice.ConformancePackInputParameter) (m map[string]string) {
m = make(map[string]string)
for _, p := range parameters {
m[*p.ParameterName] = *p.ParameterValue
}
return
}
Loading

0 comments on commit 76758ea

Please sign in to comment.