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

Implement optional support for "name" and "id" fields in data sources #53

Merged
merged 9 commits into from
Nov 27, 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
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
Loading