Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Fix provider vs current APIs #136

Merged
merged 7 commits into from
May 2, 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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OPENVPN_HOST=
OVPN_HOST=
CLOUDCONNEXA_CLIENT_ID=
arslanbekov marked this conversation as resolved.
Show resolved Hide resolved
CLOUDCONNEXA_CLIENT_SECRET=
TF_ACC=0
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ main
terraform.tfstate**
*.tfvars

*.env
*.env
/.vscode
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
HOSTNAME=cloudconnexa.dev
NAMESPACE=openvpn
NAME=cloudconnexa
VERSION=0.0.11
VERSION=0.0.12
BINARY=terraform-provider-${NAME}
OS_ARCH=darwin_arm64

Expand Down
92 changes: 0 additions & 92 deletions client/client.go

This file was deleted.

104 changes: 104 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package client
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may it is better to have something like this in https://github.com/OpenVPN/cloudconnexa-go-client


import (
"fmt"
"os"
"testing"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func validateEnvVars(t *testing.T) {
validateEnvVar(t, HostEnvVar)
validateEnvVar(t, ClientIDEnvVar)
validateEnvVar(t, ClientSecretEnvVar)
}

func validateEnvVar(t *testing.T, envVar string) {
fmt.Println(os.Getenv(envVar))
require.NotEmptyf(t, os.Getenv(envVar), "%s must be set", envVar)
}

const (
HostEnvVar = "OVPN_HOST"
ClientIDEnvVar = "CLOUDCONNEXA_CLIENT_ID"
ClientSecretEnvVar = "CLOUDCONNEXA_CLIENT_SECRET"
)

func TestNewClient(t *testing.T) {
c := setUpClient(t)
assert.NotEmpty(t, c.Token)
}

func setUpClient(t *testing.T) *cloudconnexa.Client {
validateEnvVars(t)
var err error
client, err := cloudconnexa.NewClient(os.Getenv(HostEnvVar), os.Getenv(ClientIDEnvVar), os.Getenv(ClientSecretEnvVar))
require.NoError(t, err)
return client
}

func TestListNetworks(t *testing.T) {
c := setUpClient(t)
response, err := c.Networks.GetByPage(0, 10)
require.NoError(t, err)
fmt.Printf("found %d networks\n", len(response.Content))
}

func TestListConnectors(t *testing.T) {
c := setUpClient(t)
response, err := c.Connectors.GetByPage(0, 10)
require.NoError(t, err)
fmt.Printf("found %d connectors\n", len(response.Content))
}

func TestCreateNetwork(t *testing.T) {
c := setUpClient(t)
connector := cloudconnexa.NetworkConnector{
Description: "test",
Name: "test",
VpnRegionId: "it-mxp",
}
route := cloudconnexa.Route{
Description: "test",
Type: "IP_V4",
Subnet: "10.189.253.64/30",
}
network := cloudconnexa.Network{
Description: "test",
Egress: false,
Name: "test",
InternetAccess: "LOCAL",
Connectors: []cloudconnexa.NetworkConnector{connector},
}
response, err := c.Networks.Create(network)
require.NoError(t, err)
fmt.Printf("created %s network\n", response.Id)
test, err := c.Routes.Create(response.Id, route)
require.NoError(t, err)
fmt.Printf("created %s route\n", test.Id)
serviceConfig := cloudconnexa.IPServiceConfig{
ServiceTypes: []string{"ANY"},
}
ipServiceRoute := cloudconnexa.IPServiceRoute{
Description: "test",
Value: "10.189.253.64/30",
}
service := cloudconnexa.IPService{
Name: "test",
Description: "test",
NetworkItemId: response.Id,
Type: "IP_SOURCE",
NetworkItemType: "NETWORK",
Config: &serviceConfig,
Routes: []*cloudconnexa.IPServiceRoute{&ipServiceRoute},
}
s, err := c.IPServices.Create(&service)
require.NoError(t, err)
fmt.Printf("created %s service\n", s.Id)
err = c.Networks.Delete(response.Id)
require.NoError(t, err)
fmt.Printf("deleted %s network\n", response.Id)
}
10 changes: 10 additions & 0 deletions cloudconnexa/data_source_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func dataSourceConnector() *schema.Resource {
Computed: true,
Description: "The IPV6 address of the connector.",
},
"profile": {
arslanbekov marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
Description: "OpenVPN profile",
},
},
}
}
Expand All @@ -62,6 +67,11 @@ func dataSourceConnectorRead(ctx context.Context, d *schema.ResourceData, m inte
d.Set("vpn_region_id", connector.VpnRegionId)
d.Set("ip_v4_address", connector.IPv4Address)
d.Set("ip_v6_address", connector.IPv6Address)
profile, err := c.Connectors.GetProfile(connector.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
d.Set("profile", profile)
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
return diags
}
5 changes: 3 additions & 2 deletions cloudconnexa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cloudconnexa

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
28 changes: 25 additions & 3 deletions cloudconnexa/resource_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudconnexa

import (
"context"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -54,6 +55,11 @@ func resourceConnector() *schema.Resource {
Computed: true,
Description: "The IPV6 address of the connector.",
},
"profile": {
arslanbekov marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
Description: "OpenVPN profile of the connector.",
},
},
}
}
Expand All @@ -76,6 +82,11 @@ func resourceConnectorCreate(ctx context.Context, d *schema.ResourceData, m inte
return diag.FromErr(err)
}
d.SetId(conn.Id)
profile, err := c.Connectors.GetProfile(conn.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
d.Set("profile", profile)
return append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Connector needs to be set up manually",
Expand All @@ -100,6 +111,11 @@ func resourceConnectorRead(ctx context.Context, d *schema.ResourceData, m interf
d.Set("network_item_id", connector.NetworkItemId)
d.Set("ip_v4_address", connector.IPv4Address)
d.Set("ip_v6_address", connector.IPv6Address)
profile, err := c.Connectors.GetProfile(connector.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
d.Set("profile", profile)
}
return diags
}
Expand All @@ -114,9 +130,9 @@ func resourceConnectorDelete(ctx context.Context, d *schema.ResourceData, m inte
return diags
}

func getConnectorSlice(connectors []cloudconnexa.Connector, networkItemId string, connectorName string) []interface{} {
func getConnectorSlice(connectors []cloudconnexa.Connector, networkItemId string, connectorName string, m interface{}) ([]interface{}, error) {
if len(connectors) == 0 {
return nil
return nil, nil
}
connectorsList := make([]interface{}, 1)
for _, c := range connectors {
Expand All @@ -129,9 +145,15 @@ func getConnectorSlice(connectors []cloudconnexa.Connector, networkItemId string
connector["vpn_region_id"] = c.VpnRegionId
connector["ip_v4_address"] = c.IPv4Address
connector["ip_v6_address"] = c.IPv6Address
client := m.(*cloudconnexa.Client)
profile, err := client.Connectors.GetProfile(c.Id)
if err != nil {
return nil, err
}
connector["profile"] = profile
connectorsList[0] = connector
break
}
}
return connectorsList
return connectorsList, nil
}
Loading
Loading