driftctl/pkg/resource/aws/aws_security_group_rule.go

111 lines
3.7 KiB
Go
Raw Normal View History

// GENERATED, DO NOT EDIT THIS FILE
package aws
2021-05-10 16:02:57 +00:00
import (
"bytes"
"fmt"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/zclconf/go-cty/cty"
"github.com/cloudskiff/driftctl/pkg/resource"
)
2021-03-25 11:13:52 +00:00
const AwsSecurityGroupRuleResourceType = "aws_security_group_rule"
type AwsSecurityGroupRule struct {
2021-03-25 11:13:52 +00:00
CidrBlocks *[]string `cty:"cidr_blocks"`
Description *string `cty:"description"`
FromPort *int `cty:"from_port"`
Id string `cty:"id" computed:"true"`
Ipv6CidrBlocks *[]string `cty:"ipv6_cidr_blocks"`
PrefixListIds *[]string `cty:"prefix_list_ids"`
Protocol *string `cty:"protocol"`
SecurityGroupId *string `cty:"security_group_id"`
Self *bool `cty:"self" diff:"-"`
SourceSecurityGroupId *string `cty:"source_security_group_id" computed:"true"`
ToPort *int `cty:"to_port"`
Type *string `cty:"type"`
CtyVal *cty.Value `diff:"-"`
}
func (r *AwsSecurityGroupRule) TerraformId() string {
return r.Id
}
func (r *AwsSecurityGroupRule) TerraformType() string {
return AwsSecurityGroupRuleResourceType
}
2021-03-25 11:13:52 +00:00
func (r *AwsSecurityGroupRule) CtyValue() *cty.Value {
return r.CtyVal
}
2021-05-10 16:02:57 +00:00
func CreateSecurityGroupRuleIdHash(attrs *resource.Attributes) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s-", *attrs.GetString("security_group_id")))
if attrs.GetInt("from_port") != nil && *attrs.GetInt("from_port") > 0 {
buf.WriteString(fmt.Sprintf("%d-", *attrs.GetInt("from_port")))
}
if attrs.GetInt("to_port") != nil && *attrs.GetInt("to_port") > 0 {
buf.WriteString(fmt.Sprintf("%d-", *attrs.GetInt("to_port")))
}
buf.WriteString(fmt.Sprintf("%s-", *attrs.GetString("protocol")))
buf.WriteString(fmt.Sprintf("%s-", *attrs.GetString("type")))
if attrs.GetSlice("cidr_blocks") != nil {
for _, v := range attrs.GetSlice("cidr_blocks") {
2021-05-10 16:02:57 +00:00
buf.WriteString(fmt.Sprintf("%s-", v))
}
}
if attrs.GetSlice("ipv6_cidr_blocks") != nil {
for _, v := range attrs.GetSlice("ipv6_cidr_blocks") {
2021-05-10 16:02:57 +00:00
buf.WriteString(fmt.Sprintf("%s-", v))
}
}
if attrs.GetSlice("prefix_list_ids") != nil {
for _, v := range attrs.GetSlice("prefix_list_ids") {
2021-05-10 16:02:57 +00:00
buf.WriteString(fmt.Sprintf("%s-", v))
}
}
if (attrs.GetBool("self") != nil && *attrs.GetBool("self")) ||
(attrs.GetString("source_security_group_id") != nil && *attrs.GetString("source_security_group_id") != "") {
if attrs.GetBool("self") != nil && *attrs.GetBool("self") {
buf.WriteString(fmt.Sprintf("%s-", *attrs.GetString("security_group_id")))
} else {
buf.WriteString(fmt.Sprintf("%s-", *attrs.GetString("source_security_group_id")))
}
buf.WriteString("-")
}
return fmt.Sprintf("sgrule-%d", hashcode.String(buf.String()))
}
func initAwsSecurityGroupRuleMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetNormalizeFunc(AwsSecurityGroupRuleResourceType, func(res *resource.AbstractResource) {
val := res.Attrs
val.DeleteIfDefault("security_group_id")
val.DeleteIfDefault("source_security_group_id")
2021-05-10 16:02:57 +00:00
// On first run, this field is set to null in state file and to "" after one refresh or apply
// This ensure that if we find a nil value we dont drift
val.DeleteIfDefault("description")
2021-05-10 16:02:57 +00:00
// If protocol is all (e.g. -1), tcp, udp, icmp or icmpv6 then we leave the resource untouched
// Else we delete the FromPort/ToPort and recreate the rule's id
switch *val.GetString("protocol") {
case "-1", "tcp", "udp", "icmp", "icmpv6":
return
}
val.SafeDelete([]string{"from_port"})
val.SafeDelete([]string{"to_port"})
id := CreateSecurityGroupRuleIdHash(val)
val.SafeSet([]string{"id"}, id)
res.Id = id
2021-05-10 16:02:57 +00:00
})
}