Skip to content

Commit

Permalink
feat: add resource statuses to every resource and implement it for vns (
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-rodrigues authored Aug 10, 2022
1 parent a2877ba commit e94cbbd
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 139 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<br>
<p align="center">
<a href="https://multy.dev?utm_source=github.com">
<img src="https://docs.multy.dev/img/multy.jpg" width="250">
<img src="https://multy.dev/logo_dark_mode.svg" width="250">
</a>
</p>

Expand Down
391 changes: 277 additions & 114 deletions api/proto/commonpb/common.pb.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions api/proto/commonpb/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,26 @@ message CommonResourceParameters {
Location location = 3;
CloudProvider cloud_provider = 4;
bool needs_update = 5;
ResourceStatus resource_status = 6;
}

message CommonChildResourceParameters {
string resource_id = 1;
bool needs_update = 2;
ResourceStatus resource_status = 3;
}

// Statuses of underlying created resources if they do not match the expected state.
message ResourceStatus {
enum Status {
UKNOWN_STATUS = 0;
NEEDS_UPDATE = 1;
NEEDS_RECREATE = 2;
NEEDS_CREATE = 3;
}
// key is a custom identifier of the underlying resource
// examples: aws_default_security_group, gcp_compute_network
map<string, Status> statuses = 1;
}

// Common messages for CREATE and UPDATE requests
Expand Down
30 changes: 21 additions & 9 deletions resources/types/aws/virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,42 @@ func (r AwsVirtualNetwork) FromState(state *output.TfState) (*resourcespb.Virtua
CloudProvider: r.GetCloud(),
NeedsUpdate: false,
}

stateResource, err := output.GetParsedById[virtual_network.AwsVpc](state, r.ResourceId)
if err != nil {
return nil, err
}
out.Name = stateResource.AwsResource.Tags["Name"]
out.CidrBlock = stateResource.CidrBlock
out.GcpOverride = r.Args.GcpOverride
out.AwsOutputs = &resourcespb.VirtualNetworkAwsOutputs{
VpcId: stateResource.AwsResource.ResourceId,
out.AwsOutputs = &resourcespb.VirtualNetworkAwsOutputs{}

statuses := map[string]commonpb.ResourceStatus_Status{}

if stateResource, exists, err := output.MaybeGetParsedById[virtual_network.AwsVpc](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.Name = stateResource.AwsResource.Tags["Name"]
out.CidrBlock = stateResource.CidrBlock
out.AwsOutputs.VpcId = stateResource.AwsResource.ResourceId
} else {
statuses["aws_vpc"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if stateResource, exists, err := output.MaybeGetParsedById[virtual_network.AwsInternetGateway](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.AwsOutputs.InternetGatewayId = stateResource.ResourceId
} else {
statuses["aws_internet_gateway"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if stateResource, exists, err := output.MaybeGetParsedById[network_security_group.AwsDefaultSecurityGroup](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.AwsOutputs.DefaultSecurityGroupId = stateResource.ResourceId
} else {
statuses["aws_default_security_group"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if len(statuses) > 0 {
out.CommonParameters.ResourceStatus = &commonpb.ResourceStatus{Statuses: statuses}
}

return out, nil
Expand Down
27 changes: 18 additions & 9 deletions resources/types/azure/virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,34 @@ func (r AzureVirtualNetwork) FromState(state *output.TfState) (*resourcespb.Virt
CloudProvider: r.GetCloud(),
NeedsUpdate: false,
}

stateResource, err := output.GetParsedById[virtual_network.AzureVnet](state, r.ResourceId)
if err != nil {
return nil, err
}
out.Name = stateResource.Name
out.CidrBlock = stateResource.AddressSpace[0]
out.GcpOverride = r.Args.GcpOverride
out.AzureOutputs = &resourcespb.VirtualNetworkAzureOutputs{
VirtualNetworkId: stateResource.AzResource.ResourceId,
out.AzureOutputs = &resourcespb.VirtualNetworkAzureOutputs{}
statuses := map[string]commonpb.ResourceStatus_Status{}

if stateResource, exists, err := output.MaybeGetParsedById[virtual_network.AzureVnet](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.Name = stateResource.Name
out.CidrBlock = stateResource.AddressSpace[0]

out.AzureOutputs.VirtualNetworkId = stateResource.AzResource.ResourceId
} else {
statuses["azure_virtual_network"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if stateResource, exists, err := output.MaybeGetParsedById[route_table.AzureRouteTable](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.AzureOutputs.LocalRouteTableId = stateResource.ResourceId
} else {
statuses["azure_local_route_table"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if len(statuses) > 0 {
out.CommonParameters.ResourceStatus = &commonpb.ResourceStatus{Statuses: statuses}
}
return out, nil
}

Expand Down
22 changes: 16 additions & 6 deletions resources/types/gcp/virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,32 @@ func (r GcpVirtualNetwork) FromState(state *output.TfState) (*resourcespb.Virtua
return out, nil
}

stateResource, err := output.GetParsedById[virtual_network.GoogleComputeNetwork](state, r.ResourceId)
if err != nil {
return nil, err
}
statuses := map[string]commonpb.ResourceStatus_Status{}

out.GcpOutputs = &resourcespb.VirtualNetworkGcpOutputs{
ComputeNetworkId: stateResource.SelfLink,
if stateResource, exists, err := output.MaybeGetParsedById[virtual_network.GoogleComputeNetwork](state, r.ResourceId); exists {
if err != nil {
return nil, err
}

out.GcpOutputs = &resourcespb.VirtualNetworkGcpOutputs{
ComputeNetworkId: stateResource.SelfLink,
}
} else {
statuses["gcp_compute_network"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if stateResource, exists, err := output.MaybeGetParsedById[network_security_group.GoogleComputeFirewall](state, r.ResourceId); exists {
if err != nil {
return nil, err
}
out.GcpOutputs.DefaultComputeFirewallId = stateResource.SelfLink
} else {
statuses["gcp_default_compute_firewall"] = commonpb.ResourceStatus_NEEDS_CREATE
}

if len(statuses) > 0 {
out.CommonParameters.ResourceStatus = &commonpb.ResourceStatus{Statuses: statuses}
}
return out, nil
}

Expand Down

0 comments on commit e94cbbd

Please sign in to comment.