Skip to content

Commit

Permalink
add provider meta schema with module_name field (#197)
Browse files Browse the repository at this point in the history
* add provider meta schema with module name

* move sourceChannel update into a helper
to be called within a resource, after learning
that the providerMeta is only available within the
context of a resource, not the provider itself.

* drop unused import

* return err

* return new client with updated source channel
  • Loading branch information
bcmdarroch authored Oct 20, 2021
1 parent e68bd42 commit 20383be
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
32 changes: 32 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package clients

import (
"errors"
"fmt"
"strings"

cloud_network "github.com/hashicorp/hcp-sdk-go/clients/cloud-network/preview/2020-09-07/client"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-network/preview/2020-09-07/client/network_service"
cloud_operation "github.com/hashicorp/hcp-sdk-go/clients/cloud-operation/preview/2020-05-05/client"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-operation/preview/2020-05-05/client/operation_service"
cloud_resource_manager "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client/organization_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/preview/2019-12-10/client/project_service"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

cloud_consul "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/preview/2021-02-04/client"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/preview/2021-02-04/client/consul_service"
Expand Down Expand Up @@ -81,3 +86,30 @@ func NewClient(config ClientConfig) (*Client, error) {

return client, nil
}

type providerMeta struct {
ModuleName string `cty:"module_name"`
}

// updateSourceChannel updates the SourceChannel of the client
func (cl *Client) UpdateSourceChannel(d *schema.ResourceData) (*Client, error) {

// Adds module metadata if any.
var m providerMeta

err := d.GetProviderMeta(&m)
if err != nil {
return cl, errors.New("failed to get provider meta")
}

if m.ModuleName != "" {
sc := cl.Config.SourceChannel
sc = strings.Join([]string{sc, fmt.Sprintf("terraform-module/%s", m.ModuleName)}, " ")
cl.Config.SourceChannel = sc

// Return a new client with the updated source channel
cl, err = NewClient(cl.Config)
}

return cl, nil
}
9 changes: 8 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func New() func() *schema.Provider {
Description: "The OAuth2 Client Secret for API operations.",
},
},
ProviderMetaSchema: map[string]*schema.Schema{
"module_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the module used with the provider. Should be set in the terraform config block of the module.",
},
},
}

p.ConfigureContextFunc = configure(p)
Expand Down Expand Up @@ -105,7 +112,7 @@ func configure(p *schema.Provider) func(context.Context, *schema.ResourceData) (
// on the provider or on each resource.
project, err := getProjectFromCredentials(ctx, client)
if err != nil {
diags = append(diags, diag.Errorf("unable to create HCP api client: %v", err)...)
diags = append(diags, diag.Errorf("unable to get project from credentials: %v", err)...)
return nil, diags
}
client.Config.OrganizationID = project.Parent.ID
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/resource_consul_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,20 @@ func resourceConsulCluster() *schema.Resource {
}
}

type providerMeta struct {
ModuleName string `cty:"module_name"`
}

func resourceConsulClusterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*clients.Client)

var err error
// Updates the source channel to include data about the module used.
client, err = client.UpdateSourceChannel(d)
if err != nil {
log.Printf("[DEBUG] Failed to update analytics with module name (%s)", err)
}

clusterID := d.Get("cluster_id").(string)
hvnID := d.Get("hvn_id").(string)
loc := &sharedmodels.HashicorpCloudLocationLocation{
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/resource_hvn_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func resourceHvnPeeringConnection() *schema.Resource {

func resourceHvnPeeringConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*clients.Client)

var err error
// Updates the source channel to include data about the module used.
client, err = client.UpdateSourceChannel(d)
if err != nil {
log.Printf("[DEBUG] Failed to update analytics with module name (%s)", err)
}

orgID := client.Config.OrganizationID
loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: orgID,
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_hvn_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ func setHVNRouteResourceData(d *schema.ResourceData, route *networkmodels.Hashic
func resourceHVNRouteImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*clients.Client)

var err error
// Updates the source channel to include data about the module used.
client, err = client.UpdateSourceChannel(d)
if err != nil {
log.Printf("[DEBUG] Failed to update analytics with module name (%s)", err)
}

idParts := strings.SplitN(d.Id(), ":", 2)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected {hvn_id}:{hvn_route_id}", d.Id())
Expand Down

0 comments on commit 20383be

Please sign in to comment.