1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package krmtotf 16 17 import ( 18 "fmt" 19 20 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp" 21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" 22 23 tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 24 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" 25 tfversion "github.com/hashicorp/terraform-provider-google-beta/version" 26 ) 27 28 // Inject the KCC identifier into the user agent for HTTP requests to GCP APIs issued from terraform provider. 29 // This is achieved by setting the following global variable provided by terraform provider. 30 // This function should only be called once in the program. 31 // 32 // Note that SetBlueprintAttribution will be used to add the blueprint attribution part into the user agent per resource 33 // if the resource has the 'cnrm.cloud.google.com/blueprint' annotation. 34 func SetUserAgentForTerraformProvider() { 35 tfversion.ProviderVersion = gcp.KCCUserAgent 36 } 37 38 // SetBlueprintAttribution sets the module name to the blueprint name on the given instance state if the resource has the 'cnrm.cloud.google.com/blueprint' annotation. 39 // As a result, the blueprint name will be added into the user agent for requests to the particular GCP resource. 40 func SetBlueprintAttribution(s *terraform.InstanceState, r *Resource, p *tfschema.Provider) *terraform.InstanceState { 41 bp, found := k8s.GetAnnotation(k8s.BlueprintAttributionAnnotation, r) 42 if !found { 43 return s 44 } 45 ret := s 46 if s == nil { 47 ret = &terraform.InstanceState{} 48 } 49 meta := map[string]interface{}{ 50 "module_name": fmt.Sprintf("blueprints/%v", bp), 51 } 52 ret.ProviderMeta = MapToCtyValWithSchema(meta, p.ProviderMetaSchema) 53 return ret 54 } 55