Skip to content

Commit

Permalink
feat: add output ids to virtual machines (#375)
Browse files Browse the repository at this point in the history
part of #184
  • Loading branch information
goncalo-rodrigues authored Jul 21, 2022
1 parent d46e6e3 commit c18b0ec
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 151 deletions.
470 changes: 385 additions & 85 deletions api/proto/resourcespb/virtual_machine.pb.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions api/proto/resourcespb/virtual_machine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ message VirtualMachineArgs {
int32 availability_zone = 16;
}

message VirtualMachineAwsOutputs {
string ec2_instance_id = 1;
string iam_instance_profile_arn = 2;
string iam_role_arn = 3;
string key_pair_arn = 4;
}

message VirtualMachineAzureOutputs {
string virtual_machine_id = 1;
string network_interface_id = 2;
string public_ip_id = 3;
}


message VirtualMachineGcpOutputs {
string compute_instance_id = 1;
string service_account_email = 2;
}

message VirtualMachineResource {
common.CommonResourceParameters common_parameters = 1;
string name = 2;
Expand All @@ -89,4 +108,7 @@ message VirtualMachineResource {
// outputs
string public_ip = 12;
string identity_id = 13;
VirtualMachineAwsOutputs aws_outputs = 19;
VirtualMachineAzureOutputs azure_outputs = 20;
VirtualMachineGcpOutputs gcp_outputs = 21;
}
2 changes: 2 additions & 0 deletions resources/output/iam/aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type AwsIamInstanceProfile struct {
*common.AwsResource `hcl:",squash" default:"name=aws_iam_instance_profile"`
Name string `hcl:"name"`
Role string `hcl:"role,expr"`

Arn string `json:"arn" hcle:"omitempty"`
}

func NewAssumeRolePolicy(services ...string) string {
Expand Down
2 changes: 2 additions & 0 deletions resources/output/virtual_machine/aws_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type AwsKeyPair struct {
*common.AwsResource `hcl:",squash" default:"name=aws_key_pair"`
KeyName string `hcl:"key_name"`
PublicKey string `hcl:"public_key"`

Arn string `json:"arn" hcle:"omitempty"`
}

type AwsAmi struct {
Expand Down
68 changes: 41 additions & 27 deletions resources/types/aws/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,12 @@ func InitVirtualMachine(vn *types.VirtualMachine) resources.ResourceTranslator[*
}

func (r AwsVirtualMachine) FromState(state *output.TfState) (*resourcespb.VirtualMachineResource, error) {
var ip string
identityId := "dryrun"
if r.Args.GeneratePublicIp {
ip = "dryrun"
}

if !flags.DryRun {
if r.Args.GeneratePublicIp {
vmResource, err := output.GetParsedById[virtual_machine.AwsEC2](state, r.ResourceId)
if err != nil {
return nil, err
}
ip = vmResource.PublicIp
}

iamRoleResource, err := output.GetParsedById[iam.AwsIamRole](state, r.ResourceId)
if err != nil {
return nil, err
}
identityId = iamRoleResource.Id
}

return &resourcespb.VirtualMachineResource{
out := &resourcespb.VirtualMachineResource{
CommonParameters: &commonpb.CommonResourceParameters{
ResourceId: r.ResourceId,
ResourceGroupId: r.Args.CommonParameters.ResourceGroupId,
Location: r.Args.CommonParameters.Location,
CloudProvider: r.Args.CommonParameters.CloudProvider,
NeedsUpdate: false,
},
Name: r.Args.Name,
NetworkInterfaceIds: r.Args.NetworkInterfaceIds,
Expand All @@ -67,10 +44,47 @@ func (r AwsVirtualMachine) FromState(state *output.TfState) (*resourcespb.Virtua
AwsOverride: r.Args.AwsOverride,
AzureOverride: r.Args.AzureOverride,
GcpOverride: r.Args.GcpOverride,
PublicIp: ip,
IdentityId: identityId,
AvailabilityZone: r.Args.AvailabilityZone,
}, nil
IdentityId: "dryrun",
}

if flags.DryRun {
return out, nil
}

vmResource, err := output.GetParsedById[virtual_machine.AwsEC2](state, r.ResourceId)
if err != nil {
return nil, err
}
out.AwsOutputs = &resourcespb.VirtualMachineAwsOutputs{
Ec2InstanceId: vmResource.ResourceId,
}

if r.Args.GeneratePublicIp {
out.PublicIp = vmResource.PublicIp
}

iamRoleResource, err := output.GetParsedById[iam.AwsIamRole](state, r.ResourceId)
if err != nil {
return nil, err
}
out.IdentityId = iamRoleResource.Id
out.AwsOutputs.IamRoleArn = iamRoleResource.Arn

iamInstanceProfileResource, err := output.GetParsedById[iam.AwsIamInstanceProfile](state, r.ResourceId)
if err != nil {
return nil, err
}
out.AwsOutputs.IamInstanceProfileArn = iamInstanceProfileResource.Arn

if stateResource, exists, err := output.MaybeGetParsedById[virtual_machine.AwsKeyPair](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.AwsOutputs.KeyPairArn = stateResource.Arn
}

return out, nil
}

type AwsCallerIdentityData struct {
Expand Down
61 changes: 35 additions & 26 deletions resources/types/azure/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,12 @@ func InitVirtualMachine(vn *types.VirtualMachine) resources.ResourceTranslator[*
}

func (r AzureVirtualMachine) FromState(state *output.TfState) (*resourcespb.VirtualMachineResource, error) {
var ip string
identityId := "dryrun"
if r.Args.GeneratePublicIp {
ip = "dryrun"
}

if !flags.DryRun {
if r.Args.GeneratePublicIp {
ipResource, err := output.GetParsedById[public_ip.AzurePublicIp](state, r.ResourceId)
if err != nil {
return nil, err
}
ip = ipResource.IpAddress
}
vmResource, err := output.GetParsedById[virtual_machine.AzureVirtualMachine](state, r.ResourceId)
if err != nil {
return nil, err
}
identityId = vmResource.Identities[0].PrincipalId
}

return &resourcespb.VirtualMachineResource{
out := &resourcespb.VirtualMachineResource{
CommonParameters: &commonpb.CommonResourceParameters{
ResourceId: r.ResourceId,
ResourceGroupId: r.Args.CommonParameters.ResourceGroupId,
Location: r.Args.CommonParameters.Location,
CloudProvider: r.Args.CommonParameters.CloudProvider,
NeedsUpdate: false,
},
Name: r.Args.Name,
NetworkInterfaceIds: r.Args.NetworkInterfaceIds,
Expand All @@ -70,9 +48,40 @@ func (r AzureVirtualMachine) FromState(state *output.TfState) (*resourcespb.Virt
AzureOverride: r.Args.AzureOverride,
GcpOverride: r.Args.GcpOverride,
AvailabilityZone: r.Args.AvailabilityZone,
PublicIp: ip,
IdentityId: identityId,
}, nil
IdentityId: "dryrun",
}

if flags.DryRun {
return out, nil
}

vmResource, err := output.GetParsedById[virtual_machine.AzureVirtualMachine](state, r.ResourceId)
if err != nil {
return nil, err
}
out.IdentityId = vmResource.Identities[0].PrincipalId

out.AzureOutputs = &resourcespb.VirtualMachineAzureOutputs{
VirtualMachineId: vmResource.ResourceId,
}

if r.Args.GeneratePublicIp {
ipResource, err := output.GetParsedById[public_ip.AzurePublicIp](state, r.ResourceId)
if err != nil {
return nil, err
}
out.PublicIp = ipResource.IpAddress
out.AzureOutputs.PublicIpId = ipResource.ResourceId
}

if stateResource, exists, err := output.MaybeGetParsedById[network_interface.AzureNetworkInterface](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.AzureOutputs.NetworkInterfaceId = stateResource.ResourceId
}

return out, nil
}

func (r AzureVirtualMachine) Translate(resources.MultyContext) ([]output.TfBlock, error) {
Expand Down
34 changes: 21 additions & 13 deletions resources/types/gcp/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,31 @@ func (r GcpVirtualMachine) FromState(state *output.TfState) (*resourcespb.Virtua
AzureOverride: r.Args.AzureOverride,
GcpOverride: r.Args.GcpOverride,
AvailabilityZone: r.Args.AvailabilityZone,
IdentityId: "dryrun",
}

if !flags.DryRun {
vm, err := output.GetParsedById[virtual_machine.GoogleComputeInstance](state, r.ResourceId)
if err != nil {
return nil, err
}
if flags.DryRun {
return out, nil
}

if r.Args.GeneratePublicIp {
out.PublicIp = vm.NetworkInterface[0].AccessConfig[0].NatIp
}
vm, err := output.GetParsedById[virtual_machine.GoogleComputeInstance](state, r.ResourceId)
if err != nil {
return nil, err
}

sa, err := output.GetParsedById[iam.GoogleServiceAccount](state, r.ResourceId)
if err != nil {
return nil, err
}
out.IdentityId = sa.Email
if r.Args.GeneratePublicIp {
out.PublicIp = vm.NetworkInterface[0].AccessConfig[0].NatIp
}

sa, err := output.GetParsedById[iam.GoogleServiceAccount](state, r.ResourceId)
if err != nil {
return nil, err
}
out.IdentityId = sa.Email

out.GcpOutputs = &resourcespb.VirtualMachineGcpOutputs{
ComputeInstanceId: vm.SelfLink,
ServiceAccountEmail: sa.Email,
}

return out, nil
Expand Down

0 comments on commit c18b0ec

Please sign in to comment.