Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/add-access-groups' into …
Browse files Browse the repository at this point in the history
…feature/add-access-groups

# Conflicts:
#	cloudconnexa/resource_access_group.go
  • Loading branch information
vladhanzha committed Dec 13, 2024
2 parents 582747a + 78d6279 commit c57b1eb
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
passphrase: ${{ secrets.PASSPHRASE }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6.0.0
uses: goreleaser/goreleaser-action@v6.1.0
with:
version: '~> v1'
args: release --clean
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
go build -v .
# run acceptance tests in a matrix with Terraform core versions
test:
if: github.event.pull_request.head.repo.name == github.repository
environment: TestingEnv
name: Matrix Test
needs: build
runs-on: ubuntu-latest
Expand Down Expand Up @@ -73,9 +75,10 @@ jobs:
- name: TF acceptance tests
timeout-minutes: 10
env:
OVPN_HOST: ${{ vars.OVPN_HOST }}
CLOUDCONNEXA_TEST_ORGANIZATION: ${{ secrets.CLOUDCONNEXA_TEST_ORGANIZATION }}
CLOUDCONNEXA_CLIENT_ID: ${{ secrets.CLOUDCONNEXA_CLIENT_ID }}
CLOUDCONNEXA_CLIENT_SECRET: ${{ secrets.CLOUDCONNEXA_CLIENT_SECRET }}
TF_ACC: "1"
CLOUDCONNEXA_TEST_ORGANIZATION: "terraform-community"
CLOUDCONNEXA_CLIENT_ID: ${{ secrets.CVPN_CLIENT_ID }}
CLOUDCONNEXA_CLIENT_SECRET: ${{ secrets.CVPN_CLIENT_SECRET }}
run: |
go test -v -cover ./cloudconnexa
61 changes: 50 additions & 11 deletions cloudconnexa/data_source_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloudconnexa

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
Expand All @@ -12,12 +14,16 @@ func dataSourceApplication() *schema.Resource {
ReadContext: dataSourceApplicationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "Application ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "Application name",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -48,14 +54,47 @@ func dataSourceApplication() *schema.Resource {
func dataSourceApplicationRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*cloudconnexa.Client)
var diags diag.Diagnostics
var name = data.Get("name").(string)
application, err := c.Applications.GetByName(name)
var application *cloudconnexa.ApplicationResponse
var err error
applicationId := data.Get("id").(string)
applicationName := data.Get("name").(string)
if applicationId != "" {
application, err = c.Applications.Get(applicationId)
if err != nil {
if strings.Contains(err.Error(), "status code: 404") {
return append(diags, diag.Errorf("Application with id %s was not found", applicationId)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if application == nil {
return append(diags, diag.Errorf("Application with id %s was not found", applicationId)...)
}
} else if applicationName != "" {
applicationsAll, err := c.Applications.List()
var applicationCount int
if err != nil {
return diag.FromErr(err)
}

if err != nil {
return diag.FromErr(err)
}
if application == nil {
return append(diags, diag.Errorf("Application with name %s was not found", name)...)
for _, app := range applicationsAll {
if app.Name == applicationName {
applicationCount++
}
}

if applicationCount == 0 {
return append(diags, diag.Errorf("Application with name %s was not found", applicationName)...)
} else if applicationCount > 1 {
return append(diags, diag.Errorf("More than 1 application with name %s was found. Please use id instead", applicationName)...)
} else {
application, err = c.Applications.GetByName(applicationName)
if err != nil {
return diag.FromErr(err)
}
}
} else {
return append(diags, diag.Errorf("Application name or id is missing")...)
}
setApplicationData(data, application)
return nil
Expand Down
73 changes: 57 additions & 16 deletions cloudconnexa/data_source_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudconnexa

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
Expand All @@ -13,13 +14,16 @@ func dataSourceConnector() *schema.Resource {
ReadContext: dataSourceConnectorRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The ID of the connector.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the connector.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the connector.",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -68,17 +72,54 @@ func dataSourceConnector() *schema.Resource {
func dataSourceConnectorRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*cloudconnexa.Client)
var diags diag.Diagnostics
name := d.Get("name").(string)
connector, err := c.Connectors.GetByName(name)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if connector == nil {
return append(diags, diag.Errorf("Connector with name %s was not found", name)...)
}
token, err := c.Connectors.GetToken(connector.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
var connector *cloudconnexa.Connector
var err error
var token string
connectorName := d.Get("name").(string)
connectorId := d.Get("id").(string)
if connectorId != "" {
connector, err = c.Connectors.GetByID(connectorId)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if connector == nil {
return append(diags, diag.Errorf("Connector with id %s was not found", connectorId)...)
}
token, err = c.Connectors.GetToken(connector.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
} else if connectorName != "" {
connectorsAll, err := c.Connectors.List()
var connectorCount int
if err != nil {
return append(diags, diag.FromErr(err)...)
}

for _, con := range connectorsAll {
if con.Name == connectorName {
connectorCount++
}
}

if connectorCount == 0 {
return append(diags, diag.Errorf("Connector with name %s was not found", connectorName)...)
} else if connectorCount > 1 {
return append(diags, diag.Errorf("More than 1 connector with name %s was found. Please use id instead", connectorName)...)
} else {
connector, err = c.Connectors.GetByName(connectorName)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
}

token, err = c.Connectors.GetToken(connector.Id)
if err != nil {
return append(diags, diag.FromErr(err)...)
}

} else {
return append(diags, diag.Errorf("Connector name or id is missing")...)
}

d.SetId(connector.Id)
Expand Down
42 changes: 30 additions & 12 deletions cloudconnexa/data_source_network.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 All @@ -14,14 +15,16 @@ func dataSourceNetwork() *schema.Resource {
ReadContext: dataSourceNetworkRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The network ID.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The network ID.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The network name.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The network name.",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -131,13 +134,28 @@ func dataSourceNetwork() *schema.Resource {
func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*cloudconnexa.Client)
var diags diag.Diagnostics
var network *cloudconnexa.Network
var err error
networkId := d.Get("id").(string)
networkName := d.Get("name").(string)
network, err := c.Networks.GetByName(networkName)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if network == nil {
return append(diags, diag.Errorf("Network with name %s was not found", networkName)...)
if networkId != "" {
network, err = c.Networks.Get(networkId)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if network == nil {
return append(diags, diag.Errorf("Network with id %s was not found", networkId)...)
}
} else if networkName != "" {
network, err = c.Networks.GetByName(networkName)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if network == nil {
return append(diags, diag.Errorf("Network with name %s was not found", networkName)...)
}
} else {
return append(diags, diag.Errorf("Network name or id is missing")...)
}
d.SetId(network.Id)
d.Set("name", network.Name)
Expand Down
51 changes: 39 additions & 12 deletions cloudconnexa/data_source_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloudconnexa

import (
"context"
"strings"

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

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -14,14 +16,16 @@ func dataSourceUserGroup() *schema.Resource {
ReadContext: dataSourceUserGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The user group ID.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The user group ID.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The user group name.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The user group name.",
},
"vpn_region_ids": {
Type: schema.TypeList,
Expand Down Expand Up @@ -66,13 +70,36 @@ func dataSourceUserGroup() *schema.Resource {
func dataSourceUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*cloudconnexa.Client)
var diags diag.Diagnostics
var userGroup *cloudconnexa.UserGroup
var err error
userGroupId := d.Get("id").(string)
userGroupName := d.Get("name").(string)
userGroup, err := c.UserGroups.GetByName(userGroupName)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if userGroup == nil {
return append(diags, diag.Errorf("User group with name %s was not found", userGroupName)...)
if userGroupId != "" {
userGroup, err = c.UserGroups.Get(userGroupId)
if err != nil {
if strings.Contains(err.Error(), "user group not found") {
return append(diags, diag.Errorf("User group with id %s was not found", userGroupId)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if userGroup == nil {
return append(diags, diag.Errorf("User group with id %s was not found", userGroupId)...)
}
} else if userGroupName != "" {
userGroup, err = c.UserGroups.GetByName(userGroupName)
if err != nil {
if strings.Contains(err.Error(), "user group not found") {
return append(diags, diag.Errorf("User group with name %s was not found", userGroupName)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if userGroup == nil {
return append(diags, diag.Errorf("User group with name %s was not found", userGroupName)...)
}
} else {
return append(diags, diag.Errorf("User group name or id is missing")...)
}
d.SetId(userGroup.ID)
d.Set("name", userGroup.Name)
Expand Down
2 changes: 1 addition & 1 deletion cloudconnexa/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ resource "cloudconnexa_network" "test" {
vpn_region_id = "fi-hel"
}
default_route {
value = "10.1.2.0/24"
subnet = "10.1.2.0/24"
type = "IP_V4"
}
}
Expand Down
6 changes: 3 additions & 3 deletions cloudconnexa/resource_user_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ provider "cloudconnexa" {
base_url = "https://%s.api.openvpn.com"
}
resource "cloudconnexa_user_group" "test" {
name = "%s"
vpn_region_ids = %s
name = "%s"
vpn_region_ids = %s
connect_auth = "AUTH"
}
`, testCloudID, userGroup.Name, idsStr)
}
7 changes: 7 additions & 0 deletions cloudconnexa/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,18 @@ func testAccCloudConnexaUserConfig(user cloudconnexa.User) string {
provider "cloudconnexa" {
base_url = "https://%s.api.openvpn.com"
}
resource "cloudconnexa_user_group" "userGroup1" {
name = "test-group"
vpn_region_ids = ["eu-central-1"]
connect_auth = "AUTH"
}
resource "cloudconnexa_user" "test" {
username = "%s"
email = "%s"
first_name = "%s"
last_name = "%s"
group_id = cloudconnexa_user_group.userGroup1.id
}
`, testCloudID, user.Username, user.Email, user.FirstName, user.LastName)
}

0 comments on commit c57b1eb

Please sign in to comment.