From 9541863df8136096e98d4ad8eac50c75ee10ffea Mon Sep 17 00:00:00 2001 From: zl0ty Date: Tue, 5 Nov 2024 11:54:43 +0100 Subject: [PATCH] id or name changes --- cloudconnexa/data_source_application.go | 43 +++++++++++++------ cloudconnexa/data_source_connector.go | 57 ++++++++++++++++++------- cloudconnexa/data_source_network.go | 42 ++++++++++++------ cloudconnexa/data_source_user_group.go | 14 +++--- 4 files changed, 110 insertions(+), 46 deletions(-) diff --git a/cloudconnexa/data_source_application.go b/cloudconnexa/data_source_application.go index 691cc97..a3196a5 100644 --- a/cloudconnexa/data_source_application.go +++ b/cloudconnexa/data_source_application.go @@ -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" @@ -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, @@ -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 diff --git a/cloudconnexa/data_source_connector.go b/cloudconnexa/data_source_connector.go index 7e8ed18..33f0528 100644 --- a/cloudconnexa/data_source_connector.go +++ b/cloudconnexa/data_source_connector.go @@ -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" @@ -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, @@ -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) diff --git a/cloudconnexa/data_source_network.go b/cloudconnexa/data_source_network.go index 155517e..782b2a4 100644 --- a/cloudconnexa/data_source_network.go +++ b/cloudconnexa/data_source_network.go @@ -2,6 +2,7 @@ package cloudconnexa import ( "context" + "github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -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, @@ -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) diff --git a/cloudconnexa/data_source_user_group.go b/cloudconnexa/data_source_user_group.go index 1d3c4c0..ff1ef57 100644 --- a/cloudconnexa/data_source_user_group.go +++ b/cloudconnexa/data_source_user_group.go @@ -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,