Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nic-association resource #64

Merged
merged 16 commits into from
Jun 10, 2022
18 changes: 14 additions & 4 deletions docs/resources/network_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ resource "multy_subnet" "subnet" {
cidr_block = "10.0.2.0/24"
virtual_network_id = multy_virtual_network.example_vn.id
}
resource "multy_public_ip" "ip" {
name = "test-ip"
cloud = "aws"
location = "eu_west_1"
}
resource "multy_network_interface" "private-nic" {
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
location = "eu_west_1"
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
public_ip_id = multy_public_ip.ip.id
location = "eu_west_1"
}
```

Expand All @@ -42,6 +48,10 @@ resource "multy_network_interface" "private-nic" {
- `name` (String) Name of Network Interface
- `subnet_id` (String) ID of `subnet` resource

### Optional

- `public_ip_id` (String) ID of `public_ip` resource

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
54 changes: 54 additions & 0 deletions docs/resources/network_interface_security_group_association.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "multy_network_interface_security_group_association Resource - terraform-provider-multy"
subcategory: ""
description: |-
Provides Multy Network Interface Security Group Association resource
---

# multy_network_interface_security_group_association (Resource)

Provides Multy Network Interface Security Group Association resource

## Example Usage

```terraform
resource "multy_network_interface" "nic" {
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
location = "eu_west_1"
}
resource "multy_network_security_group" "nsg" {
name = "test_nsg"
virtual_network_id = multy_virtual_network.vn.id
cloud = "aws"
location = "eu_west_1"
rule {
protocol = "tcp"
priority = 120
from_port = 22
to_port = 22
cidr_block = "0.0.0.0/0"
direction = "both"
}
}
resource "multy_network_interface_security_group_association" "nic-association" {
network_interface_id = multy_network_interface.nic.id
security_group_id = multy_network_security_group.nsg.id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `network_interface_id` (String) ID of `network_interface` resource
- `security_group_id` (String) ID of `security_group` resource

### Read-Only

- `id` (String) The ID of this resource.


11 changes: 3 additions & 8 deletions docs/resources/public_ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ resource "multy_network_interface" "public-nic" {
location = "eu_west_1"
}
resource "multy_public_ip" "ip" {
name = "test-ip"
network_interface_id = multy_network_interface.public-nic.id
cloud = "aws"
location = "eu_west_1"
name = "test-ip"
cloud = "aws"
location = "eu_west_1"
}
```

Expand All @@ -36,10 +35,6 @@ resource "multy_public_ip" "ip" {
- `location` (String) Location to deploy resource into. Read more about regions in [documentation](https://docs.multy.dev/regions)
- `name` (String) Name of Public IP

### Optional

- `network_interface_id` (String) Id of the network interface to associate public IP with

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
14 changes: 10 additions & 4 deletions examples/resources/multy_network_interface/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ resource "multy_subnet" "subnet" {
cidr_block = "10.0.2.0/24"
virtual_network_id = multy_virtual_network.example_vn.id
}
resource "multy_public_ip" "ip" {
name = "test-ip"
cloud = "aws"
location = "eu_west_1"
}
resource "multy_network_interface" "private-nic" {
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
location = "eu_west_1"
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
public_ip_id = multy_public_ip.ip.id
location = "eu_west_1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "multy_network_interface" "nic" {
cloud = "aws"
name = "test-private-nic"
subnet_id = multy_subnet.subnet.id
location = "eu_west_1"
}
resource "multy_network_security_group" "nsg" {
name = "test_nsg"
virtual_network_id = multy_virtual_network.vn.id
cloud = "aws"
location = "eu_west_1"
rule {
protocol = "tcp"
priority = 120
from_port = 22
to_port = 22
cidr_block = "0.0.0.0/0"
direction = "both"
}
}
resource "multy_network_interface_security_group_association" "nic-association" {
network_interface_id = multy_network_interface.nic.id
security_group_id = multy_network_security_group.nsg.id
}
7 changes: 3 additions & 4 deletions examples/resources/multy_public_ip/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ resource "multy_network_interface" "public-nic" {
location = "eu_west_1"
}
resource "multy_public_ip" "ip" {
name = "test-ip"
network_interface_id = multy_network_interface.public-nic.id
cloud = "aws"
location = "eu_west_1"
name = "test-ip"
cloud = "aws"
location = "eu_west_1"
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ require (
github.com/hashicorp/terraform-plugin-go v0.8.0
github.com/hashicorp/terraform-plugin-log v0.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/multycloud/multy v0.1.43
github.com/multycloud/multy v0.1.45
golang.org/x/exp v0.0.0-20220218215828-6cf2b201936e
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
)

//replace github.com/multycloud/multy v0.1.40 => ../multy
//replace github.com/multycloud/multy v0.1.43 => ../multy

require (
github.com/Azure/azure-sdk-for-go v59.2.0+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/multycloud/multy v0.1.43 h1:/uxVHRU1rvdhMyTqikudQef0pWVBkMDMcETFhmrPF74=
github.com/multycloud/multy v0.1.43/go.mod h1:ZRk8zUP2oOf5Jv72aLbwxqyDchzAVWvlWWXzcUn90jw=
github.com/multycloud/multy v0.1.45 h1:aRU0mn+0j/gwFkZPPEPYAtYDwwrBaEYBxx5kTj3EI9c=
github.com/multycloud/multy v0.1.45/go.mod h1:ZRk8zUP2oOf5Jv72aLbwxqyDchzAVWvlWWXzcUn90jw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
Expand Down
33 changes: 17 additions & 16 deletions multy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,23 @@ func (p *Provider) ConfigureProvider(ctx context.Context, config providerData, r

func (p *Provider) GetResources(_ context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
return map[string]tfsdk.ResourceType{
"multy_virtual_network": ResourceVirtualNetworkType{},
"multy_subnet": ResourceSubnetType{},
"multy_virtual_machine": ResourceVirtualMachineType{},
"multy_network_security_group": ResourceNetworkSecurityGroupType{},
"multy_network_interface": ResourceNetworkInterfaceType{},
"multy_public_ip": ResourcePublicIpType{},
"multy_route_table": ResourceRouteTableType{},
"multy_route_table_association": ResourceRouteTableAssociationType{},
"multy_object_storage_object": ResourceObjectStorageObjectType{},
"multy_object_storage": ResourceObjectStorageType{},
"multy_database": ResourceDatabaseType{},
"multy_vault": ResourceVaultType{},
"multy_vault_secret": ResourceVaultSecretType{},
"multy_vault_access_policy": ResourceVaultAccessPolicyType{},
"multy_kubernetes_cluster": ResourceKubernetesClusterType{},
"multy_kubernetes_node_pool": ResourceKubernetesNodePoolType{},
"multy_virtual_network": ResourceVirtualNetworkType{},
"multy_subnet": ResourceSubnetType{},
"multy_virtual_machine": ResourceVirtualMachineType{},
"multy_network_security_group": ResourceNetworkSecurityGroupType{},
"multy_network_interface": ResourceNetworkInterfaceType{},
"multy_network_interface_security_group_association": ResourceNetworkInterfaceSecurityGroupAssociationType{},
"multy_public_ip": ResourcePublicIpType{},
"multy_route_table": ResourceRouteTableType{},
"multy_route_table_association": ResourceRouteTableAssociationType{},
"multy_object_storage_object": ResourceObjectStorageObjectType{},
"multy_object_storage": ResourceObjectStorageType{},
"multy_database": ResourceDatabaseType{},
"multy_vault": ResourceVaultType{},
"multy_vault_secret": ResourceVaultSecretType{},
"multy_vault_access_policy": ResourceVaultAccessPolicyType{},
"multy_kubernetes_cluster": ResourceKubernetesClusterType{},
"multy_kubernetes_node_pool": ResourceKubernetesNodePoolType{},
}, nil
}

Expand Down
13 changes: 11 additions & 2 deletions multy/resource_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func (r ResourceNetworkInterfaceType) GetSchema(_ context.Context) (tfsdk.Schema
Required: true,
PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()},
},
"public_ip_id": {
Type: types.StringType,
Description: "ID of `public_ip` resource",
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.UseStateForUnknown()},
},
"cloud": common.CloudsSchema,
"location": common.LocationSchema,
},
Expand Down Expand Up @@ -97,6 +103,7 @@ type NetworkInterface struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
SubnetId types.String `tfsdk:"subnet_id"`
PublicIpId types.String `tfsdk:"public_ip_id"`
Cloud mtypes.EnumValue[commonpb.CloudProvider] `tfsdk:"cloud"`
Location mtypes.EnumValue[commonpb.Location] `tfsdk:"location"`
ResourceGroupId types.String `tfsdk:"resource_group_id"`
Expand All @@ -108,6 +115,7 @@ func convertToNetworkInterface(res *resourcespb.NetworkInterfaceResource) Networ
ResourceGroupId: types.String{Value: res.CommonParameters.ResourceGroupId},
Name: types.String{Value: res.Name},
SubnetId: types.String{Value: res.SubnetId},
PublicIpId: common.DefaultToNull[types.String](res.PublicIpId),
Cloud: mtypes.CloudType.NewVal(res.CommonParameters.CloudProvider),
Location: mtypes.LocationType.NewVal(res.CommonParameters.Location),
}
Expand All @@ -120,7 +128,8 @@ func convertFromNetworkInterface(plan NetworkInterface) *resourcespb.NetworkInte
Location: plan.Location.Value,
CloudProvider: plan.Cloud.Value,
},
Name: plan.Name.Value,
SubnetId: plan.SubnetId.Value,
Name: plan.Name.Value,
SubnetId: plan.SubnetId.Value,
PublicIpId: plan.PublicIpId.Value,
}
}
105 changes: 105 additions & 0 deletions multy/resource_network_interface_security_group_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package multy

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/multycloud/multy/api/proto/resourcespb"
)

type ResourceNetworkInterfaceSecurityGroupAssociationType struct{}

func (r ResourceNetworkInterfaceSecurityGroupAssociationType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Provides Multy Network Interface Security Group Association resource",
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.UseStateForUnknown()},
},
"network_interface_id": {
Type: types.StringType,
Description: "ID of `network_interface` resource",
Required: true,
PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()},
},
"security_group_id": {
Type: types.StringType,
Description: "ID of `security_group` resource",
Required: true,
PlanModifiers: []tfsdk.AttributePlanModifier{tfsdk.RequiresReplace()},
},
},
}, nil
}

func (r ResourceNetworkInterfaceSecurityGroupAssociationType) NewResource(_ context.Context, p tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
return MultyResource[NetworkInterfaceSecurityGroupAssociation]{
p: *(p.(*Provider)),
createFunc: createNetworkInterfaceSecurityGroupAssociation,
updateFunc: updateNetworkInterfaceSecurityGroupAssociation,
readFunc: readNetworkInterfaceSecurityGroupAssociation,
deleteFunc: deleteNetworkInterfaceSecurityGroupAssociation,
}, nil
}

func createNetworkInterfaceSecurityGroupAssociation(ctx context.Context, p Provider, plan NetworkInterfaceSecurityGroupAssociation) (NetworkInterfaceSecurityGroupAssociation, error) {
vn, err := p.Client.Client.CreateNetworkInterfaceSecurityGroupAssociation(ctx, &resourcespb.CreateNetworkInterfaceSecurityGroupAssociationRequest{
Resource: convertFromNetworkInterfaceSecurityGroupAssociation(plan),
})
if err != nil {
return NetworkInterfaceSecurityGroupAssociation{}, err
}
return convertToNetworkInterfaceSecurityGroupAssociation(vn), nil
}

func updateNetworkInterfaceSecurityGroupAssociation(ctx context.Context, p Provider, plan NetworkInterfaceSecurityGroupAssociation) (NetworkInterfaceSecurityGroupAssociation, error) {
vn, err := p.Client.Client.UpdateNetworkInterfaceSecurityGroupAssociation(ctx, &resourcespb.UpdateNetworkInterfaceSecurityGroupAssociationRequest{
ResourceId: plan.Id.Value,
Resource: convertFromNetworkInterfaceSecurityGroupAssociation(plan),
})
if err != nil {
return NetworkInterfaceSecurityGroupAssociation{}, err
}
return convertToNetworkInterfaceSecurityGroupAssociation(vn), nil
}

func readNetworkInterfaceSecurityGroupAssociation(ctx context.Context, p Provider, state NetworkInterfaceSecurityGroupAssociation) (NetworkInterfaceSecurityGroupAssociation, error) {
vn, err := p.Client.Client.ReadNetworkInterfaceSecurityGroupAssociation(ctx, &resourcespb.ReadNetworkInterfaceSecurityGroupAssociationRequest{
ResourceId: state.Id.Value,
})
if err != nil {
return NetworkInterfaceSecurityGroupAssociation{}, err
}
return convertToNetworkInterfaceSecurityGroupAssociation(vn), nil
}

func deleteNetworkInterfaceSecurityGroupAssociation(ctx context.Context, p Provider, state NetworkInterfaceSecurityGroupAssociation) error {
_, err := p.Client.Client.DeleteNetworkInterfaceSecurityGroupAssociation(ctx, &resourcespb.DeleteNetworkInterfaceSecurityGroupAssociationRequest{
ResourceId: state.Id.Value,
})
return err
}

type NetworkInterfaceSecurityGroupAssociation struct {
Id types.String `tfsdk:"id"`
NetworkInterfaceId types.String `tfsdk:"network_interface_id"`
SecurityGroupId types.String `tfsdk:"security_group_id"`
}

func convertToNetworkInterfaceSecurityGroupAssociation(res *resourcespb.NetworkInterfaceSecurityGroupAssociationResource) NetworkInterfaceSecurityGroupAssociation {
return NetworkInterfaceSecurityGroupAssociation{
Id: types.String{Value: res.CommonParameters.ResourceId},
NetworkInterfaceId: types.String{Value: res.NetworkInterfaceId},
SecurityGroupId: types.String{Value: res.SecurityGroupId},
}
}

func convertFromNetworkInterfaceSecurityGroupAssociation(plan NetworkInterfaceSecurityGroupAssociation) *resourcespb.NetworkInterfaceSecurityGroupAssociationArgs {
return &resourcespb.NetworkInterfaceSecurityGroupAssociationArgs{
NetworkInterfaceId: plan.NetworkInterfaceId.Value,
SecurityGroupId: plan.SecurityGroupId.Value,
}
}
Loading