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: Add resource/datasouce ovh_cloud_project_gateway_interface #697

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions ovh/data_cloud_project_gateway_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ovh

import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-framework/datasource"
)

var _ datasource.DataSourceWithConfigure = (*cloudProjectGatewayInterfaceDataSource)(nil)

func NewCloudProjectGatewayInterfaceDataSource() datasource.DataSource {
return &cloudProjectGatewayInterfaceDataSource{}
}

type cloudProjectGatewayInterfaceDataSource struct {
config *Config
}

func (d *cloudProjectGatewayInterfaceDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cloud_project_gateway_interface"
}

func (d *cloudProjectGatewayInterfaceDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.config = config
}

func (d *cloudProjectGatewayInterfaceDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = CloudProjectGatewayInterfaceDataSourceSchema(ctx)
}

func (d *cloudProjectGatewayInterfaceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data CloudProjectGatewayInterfaceReadModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) +
"/region/" + url.PathEscape(data.Region.ValueString()) +
"/gateway/" + url.PathEscape(data.Id.ValueString()) +
"/interface/" + url.PathEscape(data.InterfaceId.ValueString())

if err := d.config.OVHClient.Get(endpoint, &data); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
54 changes: 54 additions & 0 deletions ovh/data_cloud_project_gateway_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions ovh/data_cloud_project_gateway_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

var testAccCloudProjectGatewayInterfaceDataSourceConfig = `
data "ovh_me" "myaccount" {}

data "ovh_order_cart" "mycart" {
ovh_subsidiary = data.ovh_me.myaccount.ovh_subsidiary
}

data "ovh_order_cart_product_plan" "cloud" {
cart_id = data.ovh_order_cart.mycart.id
price_capacity = "renew"
product = "cloud"
plan_code = "project.2018"
}

resource "ovh_cloud_project" "cloud" {
ovh_subsidiary = data.ovh_order_cart.mycart.ovh_subsidiary
description = "Cloud project for gateway interface test"

plan {
duration = data.ovh_order_cart_product_plan.cloud.selected_price.0.duration
plan_code = data.ovh_order_cart_product_plan.cloud.plan_code
pricing_mode = data.ovh_order_cart_product_plan.cloud.selected_price.0.pricing_mode
}
}

resource "ovh_vrack_cloudproject" "attach" {
service_name = "%s"
project_id = ovh_cloud_project.cloud.project_id
}

resource "ovh_cloud_project_network_private" "mypriv" {
service_name = ovh_vrack_cloudproject.attach.project_id
vlan_id = "%d"
name = "%s"
regions = ["GRA11"]
}

resource "ovh_cloud_project_network_private_subnet" "myprivsub" {
service_name = ovh_cloud_project_network_private.mypriv.service_name
network_id = ovh_cloud_project_network_private.mypriv.id
region = "GRA11"
start = "10.0.0.2"
end = "10.0.0.8"
network = "10.0.0.0/24"
dhcp = true
}

resource "ovh_cloud_project_network_private_subnet" "my_other_privsub" {
service_name = ovh_cloud_project_network_private.mypriv.service_name
network_id = ovh_cloud_project_network_private.mypriv.id
region = "GRA11"
start = "10.0.1.10"
end = "10.0.1.254"
network = "10.0.1.0/24"
dhcp = true
}

resource "ovh_cloud_project_gateway" "gateway" {
service_name = ovh_cloud_project_network_private.mypriv.service_name
name = "%s"
model = "s"
region = ovh_cloud_project_network_private_subnet.myprivsub.region
network_id = tolist(ovh_cloud_project_network_private.mypriv.regions_attributes[*].openstackid)[0]
subnet_id = ovh_cloud_project_network_private_subnet.myprivsub.id
}

resource "ovh_cloud_project_gateway_interface" "interface" {
service_name = ovh_cloud_project_network_private.mypriv.service_name
region = ovh_cloud_project_network_private_subnet.my_other_privsub.region
id = ovh_cloud_project_gateway.gateway.id
subnet_id = ovh_cloud_project_network_private_subnet.my_other_privsub.id
}

data "ovh_cloud_project_gateway_interface" "interface" {
service_name = ovh_cloud_project_gateway_interface.interface.service_name
region = ovh_cloud_project_gateway_interface.interface.region
id = ovh_cloud_project_gateway_interface.interface.id
interface_id = ovh_cloud_project_gateway_interface.interface.interface_id
}
`

func TestAccCloudProjectGatewayInterfaceDataSource_basic(t *testing.T) {
config := fmt.Sprintf(
testAccCloudProjectGatewayInterfaceDataSourceConfig,
os.Getenv("OVH_VRACK_SERVICE_TEST"),
acctest.RandIntRange(100, 200),
acctest.RandomWithPrefix(test_prefix),
acctest.RandomWithPrefix(test_prefix),
)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_gateway_interface.interface", "ip"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_gateway_interface.interface", "subnet_id"),
resource.TestCheckResourceAttrSet("data.ovh_cloud_project_gateway_interface.interface", "network_id"),
),
},
},
})
}
2 changes: 2 additions & 0 deletions ovh/provider_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (p *OvhProvider) Configure(ctx context.Context, req provider.ConfigureReque
func (p *OvhProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewCloudProjectDatabaseIPRestrictionsDataSource,
NewCloudProjectGatewayInterfaceDataSource,
NewCloudProjectLoadbalancerDataSource,
NewCloudProjectLoadbalancersDataSource,
NewCloudProjectDataSource,
Expand All @@ -208,6 +209,7 @@ func (p *OvhProvider) DataSources(_ context.Context) []func() datasource.DataSou
func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewCloudProjectAlertingResource,
NewCloudProjectGatewayInterfaceResource,
NewDbaasLogsTokenResource,
NewDomainZoneDnssecResource,
NewIpFirewallResource,
Expand Down
9 changes: 5 additions & 4 deletions ovh/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/url"
"os"
"testing"

Expand Down Expand Up @@ -379,7 +380,7 @@ func testAccCheckVRackExists(t *testing.T) {

r := vrackResponse{}

endpoint := fmt.Sprintf("/vrack/%s", os.Getenv("OVH_VRACK_SERVICE_TEST"))
endpoint := fmt.Sprintf("/vrack/%s", url.PathEscape(os.Getenv("OVH_VRACK_SERVICE_TEST")))

err := testAccOVHClient.Get(endpoint, &r)
if err != nil {
Expand All @@ -396,7 +397,7 @@ func testAccCheckCloudProjectExists(t *testing.T) {

r := cloudProjectResponse{}

endpoint := fmt.Sprintf("/cloud/project/%s", os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST"))
endpoint := fmt.Sprintf("/cloud/project/%s", url.PathEscape(os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")))

err := testAccOVHClient.Get(endpoint, &r)
if err != nil {
Expand All @@ -413,7 +414,7 @@ func testAccCheckIpLoadbalancingExists(t *testing.T) {

r := iplbResponse{}

endpoint := fmt.Sprintf("/ipLoadbalancing/%s", os.Getenv("OVH_IPLB_SERVICE_TEST"))
endpoint := fmt.Sprintf("/ipLoadbalancing/%s", url.PathEscape(os.Getenv("OVH_IPLB_SERVICE_TEST")))

err := testAccOVHClient.Get(endpoint, &r)
if err != nil {
Expand All @@ -429,7 +430,7 @@ func testAccCheckDomainZoneExists(t *testing.T) {

r := domainZoneResponse{}

endpoint := fmt.Sprintf("/domain/zone/%s", os.Getenv("OVH_ZONE_TEST"))
endpoint := fmt.Sprintf("/domain/zone/%s", url.PathEscape(os.Getenv("OVH_ZONE_TEST")))

err := testAccOVHClient.Get(endpoint, &r)
if err != nil {
Expand Down
Loading