PulumiをGolangで作成しています。
おおよその機能はGo言語で書くことができたのですが、配列からループしてPulumiの型に沿った配列を作るときにエラーが出てしまいます。
エラー内容は下記です。
Intellijのエディターの段階でエラーが出てしまいます。
Cannot use 'ec2.SecurityGroupIngressArgs{ Protocol: pulumi.String(v.Protocol), ToPort: pulumi.Int(v.ToPort), FromPort: pulumi.Int(v.FromPort), Description: pulumi.String(v.Description), CidrBlocks: pulumi.StringArray{ pulumi.String("0.0.0.0/0"), }, }' (type SecurityGroupIngressArgs) as type ec2.SecurityGroupIngressArray
問題の箇所です。
SecurityGroupIngressArgsの型を踏襲して配列を生成したい。
Go
1var createdEgress []ec2.SecurityGroupIngressArray 2 3 for _, v := range region.SecurityGroup.Ingress { 4 createdEgress = append(createdEgress, ec2.SecurityGroupIngressArgs{ 5 Protocol: pulumi.String(v.Protocol), 6 ToPort: pulumi.Int(v.ToPort), 7 FromPort: pulumi.Int(v.FromPort), 8 Description: pulumi.String(v.Description), 9 CidrBlocks: pulumi.StringArray{ 10 pulumi.String("0.0.0.0/0"), 11 }, 12 }) 13 //createdEgress[i] = ec2.SecurityGroupIngressArgs{ 14 // Protocol: pulumi.String(v.Protocol), 15 // ToPort: pulumi.Int(v.ToPort), 16 // FromPort: pulumi.Int(v.FromPort), 17 // Description: pulumi.String(v.Description), 18 //} 19 }
region.SecurityGroup.Ingressの中身は下記です。
jsonからUnmarshalをしてparseしています。
中身をちゃんと取得できているのは確認済です。
json
1{ 2 "ResourceName": "superTest", 3 "vpc": { 4 "cidr": "172.16.0.0/16", 5 "tag": "vpcTest" 6 }, 7 "subnet": { 8 "cidr": "172.16.10.0/24", 9 "availabilityZone": "us-west-2a", 10 "tag": "subnetTest" 11 }, 12 "networkInterface": { 13 "privateIp": "172.16.10.100", 14 "tag": "networkInterfaceTest" 15 }, 16 "securityGroup": { 17 "ingress": [ 18 { 19 "protocol": "tcp", 20 "toPort": 22, 21 "fromPort": 22, 22 "description": "Allow inbound TCP 22", 23 "cidrBlocks": [ 24 "0.0.0.0/0" 25 ] 26 } 27 ], 28 "egress": [ 29 { 30 "protocol": "-1", 31 "toPort": 0, 32 "fromPort": 0, 33 "description": "Allow all outbound traffic", 34 "cidrBlocks": [ 35 "0.0.0.0/0" 36 ] 37 } 38 ] 39 }, 40 "instance": { 41 "ami": "ami-02701bcdc5509e57b", 42 "instanceType": "t2.xlarge", 43 "tag": "instanceTest" 44 } 45} 46
実際のコードです。
Go
1package resource 2 3import ( 4 "fmt" 5 "github.com/pulumi/pulumi-aws/sdk/v3/go/aws/ec2" 6 "github.com/pulumi/pulumi/sdk/v2/go/pulumi" 7) 8 9type Region struct { 10 ResourceName string `json:"ResourceName"` 11 Location string `json:"Location"` 12 Vpc *Vpc `json:"vpc"` 13 Subnet *Subnet `json:"subnet"` 14 NetworkInterface *NetworkInterface `json:"networkInterface"` 15 SecurityGroup *SecurityGroup `json:"SecurityGroup"` 16 Instance *Instance `json:"instance"` 17} 18 19type Vpc struct { 20 Cidr string `json:"cidr"` 21 Tag string `json:"tag"` 22} 23 24type Subnet struct { 25 Cidr string `json:"cidr"` 26 AvailabilityZone string `json:"availabilityZone"` 27 Tag string `json:"tag"` 28} 29 30type NetworkInterface struct { 31 PrivateIp string `json:"privateIp"` 32 Tag string `json:"tag"` 33} 34 35type SecurityGroup struct { 36 Ingress []Ingress `json:"ingress"` 37 Egress []Egress `json:"egress"` 38} 39 40type Ingress struct { 41 Protocol string `json:"protocol"` 42 ToPort int `json:"toPort"` 43 FromPort int `json:"fromPort"` 44 Description string `json:"description"` 45 CidrBlocks []string `json:"cidrBlocks"` 46} 47 48type Egress struct { 49 Protocol string `json:"protocol"` 50 ToPort int `json:"toPort"` 51 FromPort int `json:"fromPort"` 52 Description string `json:"description"` 53 CidrBlocks []string `json:"cidrBlocks"` 54} 55 56type Instance struct { 57 AMI string `json:"ami"` 58 InstanceType string `json:"instanceType"` 59 Tag string `json:"tag"` 60} 61 62type Deployment struct { 63} 64 65func newRegion(region Region) *Region { 66 return &Region{ 67 ResourceName: region.ResourceName, 68 Location: region.Location, 69 Vpc: region.Vpc, 70 Subnet: region.Subnet, 71 NetworkInterface: region.NetworkInterface, 72 SecurityGroup: region.SecurityGroup, 73 Instance: region.Instance, 74 } 75} 76 77func (d *Deployment) createNewVpc( 78 ctx *pulumi.Context, 79 region *Region, 80 ) (*ec2.Vpc, error) { 81 newVpc, err := ec2.NewVpc(ctx, 82 fmt.Sprintf("%s%s", region.ResourceName, "-vpc"), 83 &ec2.VpcArgs{ 84 CidrBlock: pulumi.String(region.Vpc.Cidr), 85 Tags: pulumi.StringMap{ 86 "Name": pulumi.String(region.Vpc.Tag), 87 }, 88 }) 89 90 if err != nil { 91 return nil, err 92 } 93 94 return newVpc, nil 95} 96 97func (d *Deployment) createNewSubnet( 98 ctx *pulumi.Context, 99 region *Region, 100 newVpc *ec2.Vpc, 101 ) (*ec2.Subnet, error) { 102 newSubnet, err := ec2.NewSubnet(ctx, 103 fmt.Sprintf("%s%s", region.ResourceName, "-subnet"), 104 &ec2.SubnetArgs{ 105 VpcId: newVpc.ID(), 106 CidrBlock: pulumi.String(region.Subnet.Cidr), 107 AvailabilityZone: pulumi.String(region.Subnet.AvailabilityZone), 108 Tags: pulumi.StringMap{ 109 "Name": pulumi.String(region.Subnet.Tag), 110 }, 111 }) 112 113 if err != nil { 114 return nil, err 115 } 116 117 return newSubnet, nil 118} 119 120func (d *Deployment) createNetworkInterface( 121 ctx *pulumi.Context, 122 region *Region, 123 newSubnet *ec2.Subnet, 124) (*ec2.NetworkInterface, error) { 125 networkInterface, err := ec2.NewNetworkInterface(ctx, 126 fmt.Sprintf("%s%s", region.ResourceName, "-networkInterface"), 127 &ec2.NetworkInterfaceArgs{ 128 SubnetId: newSubnet.ID(), 129 PrivateIps: pulumi.StringArray{ 130 pulumi.String(region.NetworkInterface.PrivateIp), 131 }, 132 Tags: pulumi.StringMap{ 133 "Name": pulumi.String(region.NetworkInterface.Tag), 134 }, 135 }) 136 137 if err != nil { 138 return nil, err 139 } 140 141 return networkInterface, nil 142} 143 144func (d *Deployment) createNewInstance( 145 ctx *pulumi.Context, 146 region *Region, 147 newNetworkInterface *ec2.NetworkInterface, 148 newSecurityGroup *ec2.SecurityGroup, 149) (*ec2.Instance, error) { 150 instance, err := ec2.NewInstance(ctx, 151 fmt.Sprintf("%s%s", region.ResourceName, "Instance"), 152 &ec2.InstanceArgs{ 153 VpcSecurityGroupIds: pulumi.StringArray{newSecurityGroup.Name}, 154 Ami: pulumi.String(region.Instance.AMI), 155 InstanceType: pulumi.String(region.Instance.InstanceType), 156 NetworkInterfaces: ec2.InstanceNetworkInterfaceArray{ 157 &ec2.InstanceNetworkInterfaceArgs{ 158 NetworkInterfaceId: newNetworkInterface.ID(), 159 DeviceIndex: pulumi.Int(0), 160 }, 161 }, 162 }) 163 164 if err != nil { 165 return nil, err 166 } 167 168 return instance, nil 169} 170 171func (d *Deployment) createNewSecurityGroup( 172 ctx *pulumi.Context, 173 region *Region, 174 newVpc *ec2.Vpc, 175) (*ec2.SecurityGroup, error) { 176 var createdEgress []ec2.SecurityGroupIngressArray 177 178 ↓ ここでJsonから取得したデータをcreatedEgressに対して配列pushしたい 179 for _, v := range region.SecurityGroup.Ingress { 180 createdEgress = append(createdEgress, ec2.SecurityGroupIngressArgs{ 181 Protocol: pulumi.String(v.Protocol), 182 ToPort: pulumi.Int(v.ToPort), 183 FromPort: pulumi.Int(v.FromPort), 184 Description: pulumi.String(v.Description), 185 CidrBlocks: pulumi.StringArray{ 186 pulumi.String("0.0.0.0/0"), 187 }, 188 }) 189 //createdEgress[i] = ec2.SecurityGroupIngressArgs{ 190 // Protocol: pulumi.String(v.Protocol), 191 // ToPort: pulumi.Int(v.ToPort), 192 // FromPort: pulumi.Int(v.FromPort), 193 // Description: pulumi.String(v.Description), 194 //} 195 } 196 197 securityGroup, err := ec2.NewSecurityGroup(ctx, 198 "ssh-sg", 199 &ec2.SecurityGroupArgs{ 200 Name: pulumi.String("ssh-sg"), 201 VpcId: newVpc.ID(), 202 Description: pulumi.String("Allows SSH traffic to bastion hosts"), 203 Ingress: ec2.SecurityGroupIngressArray{ 204 ec2.SecurityGroupIngressArgs{ ← ここにcreatedEgressをセットしたい 205 Protocol: pulumi.String(region.SecurityGroup.Ingress[0].Protocol), 206 ToPort: pulumi.Int(region.SecurityGroup.Ingress[0].ToPort), 207 FromPort: pulumi.Int(region.SecurityGroup.Ingress[0].FromPort), 208 Description: pulumi.String(region.SecurityGroup.Ingress[0].Description), 209 CidrBlocks: pulumi.StringArray{ 210 pulumi.String(region.SecurityGroup.Ingress[0].CidrBlocks[0]), 211 }, 212 }, 213 }, 214 Egress: ec2.SecurityGroupEgressArray{ 215 ec2.SecurityGroupEgressArgs{ 216 Protocol: pulumi.String(region.SecurityGroup.Egress[0].Protocol), 217 ToPort: pulumi.Int(region.SecurityGroup.Egress[0].ToPort), 218 FromPort: pulumi.Int(region.SecurityGroup.Egress[0].FromPort), 219 Description: pulumi.String(region.SecurityGroup.Egress[0].Description), 220 CidrBlocks: pulumi.StringArray{ 221 pulumi.String(region.SecurityGroup.Egress[0].CidrBlocks[0]), 222 }, 223 }, 224 }, 225 }) 226 227 if err != nil { 228 return nil, err 229 } 230 231 return securityGroup, nil 232} 233
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/24 08:18