Skip to content

Commit

Permalink
structs for useragent
Browse files Browse the repository at this point in the history
  • Loading branch information
drewmullen committed Jun 3, 2022
1 parent 7134ce9 commit 78ef3c5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ website/node_modules
.idea
*.iml
*.test
*__debug_bin

website/vendor

Expand Down
37 changes: 36 additions & 1 deletion internal/acctest/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,49 @@ terraform {
name = "my-test-module"
version = "0.0.1"
comment = "testing user-agent comment"
}]
},
{
name = "2nd-user-agent"
version = "0.0.1"
comment = "2nd user agent"
}
]
}
}
`, role) + config
}
return config
}

func (td *TestData) MetadataConfig() string {
config := fmt.Sprintf(`
resource %[1]q %[2]q {}
`, td.TerraformResourceType, td.ResourceLabel)

config = fmt.Sprintf(`
resource awscc_ec2_vpc drew {
cidr_block = "10.0.0.0/16"
}
terraform {
provider_meta "awscc" {
user_agent = [{
product_name = "my-test-module"
product_version = "0.0.1"
comment = "testing user-agent comment"
},
{
product_name = "2nd-user-agent"
product_version = "0.0.1"
comment = "2nd user agent"
}
]
}
}
` + config)
return config
}

// DataSourceWithEmptyResourceConfig returns a Terraform configuration for the data source and its respective resource.
func (td *TestData) DataSourceWithEmptyResourceConfig() string {
return td.EmptyConfig() + fmt.Sprintf(`
Expand Down
19 changes: 19 additions & 0 deletions internal/aws/ec2/vpc_resource_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion internal/generic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
Expand Down Expand Up @@ -394,11 +395,33 @@ var (
idAttributePath = tftypes.NewAttributePath().WithAttributeName("id")
)

type meta struct {
UserAgents []userAgentProduct `tfsdk:"user_agent"`
}

type userAgentProduct struct {
ProductName types.String `tfsdk:"product_name"`
ProductVersion types.String `tfsdk:"product_version"`
Comment types.String `tfsdk:"comment"`
}

func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceRequest, response *tfsdk.CreateResourceResponse) {
ctx = r.cfnTypeContext(ctx)

traceEntry(ctx, "Resource.Create")

var m []meta
providerMeta := request.ProviderMeta

diags := providerMeta.Get(ctx, &m)

// userAgentProducts

//(*(*r).resourceType).cfTypeName == "AWS::EC2::VPC"
//providerMeta.Raw.value != nil
// newCtx := context.WithValue(ctx, "meta", m)

// conn := r.provider.CloudControlApiClient(newCtx)
conn := r.provider.CloudControlApiClient(ctx)

tflog.Debug(ctx, "Request.Plan.Raw", map[string]interface{}{
Expand Down Expand Up @@ -480,7 +503,7 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
}
}

diags := r.populateUnknownValues(ctx, id, &response.State)
diags = r.populateUnknownValues(ctx, id, &response.State)

if diags.HasError() {
response.Diagnostics.Append(diags...)
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ type providerData struct {
UserAgent []userAgentProduct `tfsdk:"user_agent"`
terraformVersion string
}
type providerMeta struct {
UserAgent []userAgentProduct `tfsdk:"user_agent"`
}

type userAgentProduct struct {
ProductName types.String `tfsdk:"product_name"`
Expand Down Expand Up @@ -508,6 +511,8 @@ func (p *AwsCloudControlApiProvider) RoleARN(_ context.Context) string {

// newCloudControlClient configures and returns a fully initialized AWS Cloud Control API client with the configured region.
func newCloudControlClient(ctx context.Context, pd *providerData) (*cloudcontrol.Client, string, error) {
// m, _ := ctx.Value("meta").(*tfsdk.Config) // _ was ok

config := awsbase.Config{
AccessKey: pd.AccessKey.Value,
CallerDocumentationURL: "https://registry.terraform.io/providers/hashicorp/awscc",
Expand All @@ -527,6 +532,7 @@ func newCloudControlClient(ctx context.Context, pd *providerData) (*cloudcontrol
},
}
config.UserAgent = userAgentProducts(pd.UserAgent)
// config.UserAgent = userAgentProducts(m.Schema.)
if pd.MaxRetries.Null {
config.MaxRetries = defaultMaxRetries
} else {
Expand Down

0 comments on commit 78ef3c5

Please sign in to comment.