Skip to content

Commit

Permalink
id or name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zl0ty committed Nov 5, 2024
1 parent 75539ba commit 9541863
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 46 deletions.
43 changes: 31 additions & 12 deletions cloudconnexa/data_source_application.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 @@ -12,12 +13,16 @@ func dataSourceApplication() *schema.Resource {
ReadContext: dataSourceApplicationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
Description: "Application ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
Description: "Application name",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -48,14 +53,28 @@ 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)

if err != nil {
return diag.FromErr(err)
}
if application == nil {
return append(diags, diag.Errorf("Application with name %s was not found", 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 {
return diag.FromErr(err)
}
if application == nil {
return append(diags, diag.Errorf("Application with id %s was not found", applicationId)...)
}
} else if applicationName != "" {
application, err = c.Applications.GetByName(applicationName)
if err != nil {
return diag.FromErr(err)
}
if application == nil {
return append(diags, diag.Errorf("Application with name %s was not found", applicationName)...)
}
} else {
return append(diags, diag.Errorf("Application name or id is missing")...)
}
setApplicationData(data, application)
return nil
Expand Down
57 changes: 41 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,
AtLeastOneOf: []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,
AtLeastOneOf: []string{"id", "name"},
Description: "The name of the connector.",
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -68,17 +72,38 @@ 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 != "" {
connector, err = c.Connectors.GetByName(connectorName)
if err != nil {
return append(diags, diag.FromErr(err)...)
}
if connector == nil {
return append(diags, diag.Errorf("Connector with name %s was not found", connectorName)...)
}
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,
AtLeastOneOf: []string{"id", "name"},
Description: "The network ID.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The network name.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []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
14 changes: 8 additions & 6 deletions cloudconnexa/data_source_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ func dataSourceUserGroup() *schema.Resource {
ReadContext: dataSourceUserGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The user group ID.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
Description: "The user group ID.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The user group name.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
Description: "The user group name.",
},
"vpn_region_ids": {
Type: schema.TypeList,
Expand Down

0 comments on commit 9541863

Please sign in to comment.