Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,26 @@ ifneq ($(SKIP_CLEAN),)
endif
endif

terraform build provider: validate_environment clean-provider mmv1 tpgtools
TEMP_DIR := $(shell mktemp -d)
export TEMP_DIR

terraform build provider: validate_environment clean-provider mmv1 tpgtools clean-temp
@echo "Provider generation process finished for $(VERSION) in $(OUTPUT_PATH)"


mmv1:
mmv1: prepare-temp
@echo "Executing mmv1 build for $(OUTPUT_PATH)";
@cd mmv1;\
@cd $(TEMP_DIR)/mmv1;\
if [ "$(VERSION)" = "ga" ]; then \
go run . --output $(OUTPUT_PATH) --version ga --no-docs $(mmv1_compile) \
&& go run . --output $(OUTPUT_PATH) --version beta --no-code $(mmv1_compile); \
else \
go run . --output $(OUTPUT_PATH) --version $(VERSION) $(mmv1_compile); \
fi

tpgtools: serialize
tpgtools: prepare-temp serialize
@echo "Executing tpgtools build for $(OUTPUT_PATH)";
@cd tpgtools;\
@cd $(TEMP_DIR)/tpgtools;\
go run . --output $(OUTPUT_PATH) --version $(VERSION) $(tpgtools_compile); \
rm serialization.go

Expand Down Expand Up @@ -122,22 +125,24 @@ clean-tgc:
rm -rf ./test/*;\
rm -rf ./cmd/*;\

tgc:
cd mmv1;\
tgc: prepare-temp
cd $(TEMP_DIR)/mmv1;\
go run . --version beta --provider tgc --output $(OUTPUT_PATH)/tfplan2cai $(mmv1_compile)\
&& go run . --version ga --provider tgc_cai2hcl --output $(OUTPUT_PATH)/cai2hcl $(mmv1_compile)\
&& go run . --version ga --provider tgc_next --output $(OUTPUT_PATH) $(mmv1_compile);\
&& go run . --version ga --provider tgc_next --output $(OUTPUT_PATH) $(mmv1_compile);
$(MAKE) clean-temp\

tf-oics:
cd mmv1;\
go run . --version ga --provider oics --output $(OUTPUT_PATH) $(mmv1_compile);\
tf-oics: prepare-temp
cd $(TEMP_DIR)/mmv1;\
go run . --version ga --provider oics --output $(OUTPUT_PATH) $(mmv1_compile);
$(MAKE) clean-temp\

test:
cd mmv1; \
go test ./...

serialize:
cd tpgtools;\
cd $(TEMP_DIR)/tpgtools;\
cp -f serialization.go.base serialization.go &&\
go run . $(serialize_compile) --mode "serialization" > temp.serial &&\
mv -f temp.serial serialization.go
Expand Down Expand Up @@ -175,4 +180,36 @@ check_safe_build:
doctor:
./scripts/doctor

.PHONY: mmv1 tpgtools test clean-provider validate_environment serialize doctor

prepare-temp:

@echo "Setting up temporary workspace for conversion at $(TEMP_DIR)..."
@rm -rf $(TEMP_DIR)
@mkdir -p $(TEMP_DIR)/mmv1 $(TEMP_DIR)/tpgtools $(TEMP_DIR)/tools
@echo "Copying files to $(TEMP_DIR)..."
@cp -R ./mmv1/. $(TEMP_DIR)/mmv1/
@cp -R ./tpgtools/. $(TEMP_DIR)/tpgtools/
@cp -R ./tools/. $(TEMP_DIR)/tools/
@echo "Building resource template converter in temp..."
cd $(TEMP_DIR)/tools/resource-template-converter && go env -w GO111MODULE=on && go mod tidy && go build -v -o $(TEMP_DIR)/tools/convert-resource-template . && echo "BUILD SUCCEEDED" || (echo "BUILD FAILED"; exit 1)
@echo "Build finished, running converter..."
@ls -la $(TEMP_DIR)/tools/convert-resource-template
@$(TEMP_DIR)/tools/convert-resource-template convert-resource-template $(TEMP_DIR)
@echo "Temporary workspace setup complete."

test-convert:
@echo $(TEMP_DIR)

clean-temp:
@echo "Cleaning up temporary workspace $(TEMP_DIR)..."
@rm -rf $(TEMP_DIR)

convert-templates:
@echo "Checking and running resource template converter..."
@if [ ! -f ./convert-resource-template ]; then \
echo "Building convert-resource-template tool..."; \
(cd tools/resource-template-converter && go build -o ../../convert-resource-template); \
fi
@./convert-resource-template convert-resource-template .

.PHONY: mmv1 tpgtools test clean-provider validate_environment serialize doctor convert-templates prepare-temp clean-temp
3 changes: 3 additions & 0 deletions mmv1/api/resource/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Sample struct {
// Whether to skip generating tests for this resource
ExcludeTest bool `yaml:"exclude_test,omitempty"`

// Whether to EXCLUDE the first step from doc generation
ExcludeBasicDoc bool `yaml:"exclude_basic_doc,omitempty"`

// Specify which external providers are needed for the testcase.
// Think before adding as there is latency and adds an external dependency to
// your test so avoid if you can.
Expand Down
41 changes: 30 additions & 11 deletions mmv1/api/resource/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -106,13 +107,18 @@ type Step struct {
// Whether to skip import tests for this test step
ExcludeImportTest bool `yaml:"exclude_import_test,omitempty"`

// Whether to skip generating docs for this test step
ExcludeDocs bool `yaml:"exclude_docs,omitempty"`
// Whether to generate docs for this test step (override sample's exclude_basic_doc)
IncludeStepDoc bool `yaml:"include_step_doc,omitempty"`

DocumentationHCLText string `yaml:"-"`
TestHCLText string `yaml:"-"`
OicsHCLText string `yaml:"-"`
PrimaryResourceId string `yaml:"-"`
DocumentationHCLText string `yaml:"-"`
TestHCLText string `yaml:"-"`
OicsHCLText string `yaml:"-"`
PrimaryResourceId string `yaml:"-"`
TestContextVars map[string]string `yaml:"-"`
}

func (s *Step) ShouldGenerateDoc(index int, sample *Sample) bool {
return s.IncludeStepDoc || (index == 0 && !sample.ExcludeBasicDoc)
}

func (s *Step) TestStepSlug(productName, resourceName string) string {
Expand Down Expand Up @@ -190,6 +196,7 @@ func (s *Step) SetHCLText(sysfs fs.FS) {
testPrefixedVars := make(map[string]string)
testVars := make(map[string]string)
testTestEnvVars := make(map[string]string)
testContextVars := make(map[string]string)
// Override prefixed_vars to inject test values into configs - will have
// - "a-example-var-value%{random_suffix}""
// - "%{my_var}" for overrides that have custom Golang values
Expand All @@ -207,17 +214,25 @@ func (s *Step) SetHCLText(sysfs fs.FS) {
if len(newVal) > 54 {
newVal = newVal[:54]
}
testPrefixedVars[key] = fmt.Sprintf("%s%%{random_suffix}", newVal)
// testPrefixedVars[key] = fmt.Sprintf("%s%%{random_suffix}", newVal)
testPrefixedVars[key] = fmt.Sprintf("%%{%s}", key)
testContextVars[key] = fmt.Sprintf("\"%s\"+randomSuffix", newVal)
}

for key := range originalVars {
for key, value := range originalVars {
testVars[key] = fmt.Sprintf("%%{%s}", key)
if _, err := strconv.ParseBool(value); err == nil {
testContextVars[key] = value
} else if _, err := strconv.ParseInt(value, 10, 64); err == nil {
testContextVars[key] = value
} else {
testContextVars[key] = fmt.Sprintf("%#v", value)
}
}

// Apply overrides from YAML
for key := range s.TestVarsOverrides {
testPrefixedVars[key] = fmt.Sprintf("%%{%s}", key)
testVars[key] = fmt.Sprintf("%%{%s}", key)
for key, value := range s.TestVarsOverrides {
testContextVars[key] = fmt.Sprintf("%s", value)
}

for key := range originalTestEnvVars {
Expand All @@ -227,6 +242,7 @@ func (s *Step) SetHCLText(sysfs fs.FS) {
s.PrefixedVars = testPrefixedVars
s.TestEnvVars = testTestEnvVars
s.Vars = testVars
s.TestContextVars = testContextVars
s.TestHCLText = s.ExecuteTemplate(sysfs)
s.TestHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.TestHCLText, "\n")
// Remove region tags
Expand Down Expand Up @@ -326,6 +342,9 @@ func (s *Step) SetOiCSHCLText(sysfs fs.FS) {
for key, value := range originalPrefixedVars {
testPrefixedVars[key] = fmt.Sprintf("%s-${local.name_suffix}", value)
}
for key, value := range originalVars {
testVars[key] = value
}

// Apply overrides from YAML
for key, value := range s.OicsVarsOverrides {
Expand Down
24 changes: 24 additions & 0 deletions mmv1/products/cloudrunv2/Service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ examples:
ignore_read_extra:
- 'deletion_protection'
exclude_docs: true
samples:
- name: cloudrunv2_service_iap_update
primary_resource_id: default
# exclude_basic_doc: true
steps:
- name: cloudrunv2_service_iap_update
prefixed_vars:
cloud_run_service_name: 'cloudrun-iap-service'
vars:
test_name: 'test-name'
iap_enabled: 'true'
max_instance_count: '100'
ignore_read_extra:
- deletion_protection
- name: cloudrunv2_service_iap_update
prefixed_vars:
cloud_run_service_name: 'cloudrun-iap-service'
vars:
test_name: 'test_name'
iap_enabled: 'false'
max_instance_count: '100'
ignore_read_extra:
- deletion_protection
include_step_doc: true
virtual_fields:
- name: 'deletion_protection'
description: |
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/compute/GlobalForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
subnetwork_name: 'my-subnetwork'
default_backend_service_name: 'backend-default'
hybrid_backend_service_name: 'backend-hybrid'
internal_backend_service_name": 'backend-internal'
internal_backend_service_name: 'backend-internal'
default_neg_name: 'default-neg'
hybrid_neg_name: 'hybrid-neg'
internal_neg_name: 'internal-neg'
Expand Down Expand Up @@ -413,7 +413,7 @@
For Private Service Connect forwarding rules that forward traffic to Google
APIs, a network must be provided.
default_from_api: true
# TODO: When implementing new types enable converting the

Check warning on line 416 in mmv1/products/compute/GlobalForwardingRule.yaml

View workflow job for this annotation

GitHub Actions / lint-yaml

416:3 [comments-indentation] comment not indented like content
# manifest input from a single value to a range of form NN-NN. The API
# accepts a single value, e.g. '80', but the API stores and returns
# '80-80'. This causes idempotency false positive.
Expand Down
8 changes: 5 additions & 3 deletions mmv1/templates/terraform/iam/iam_test_setup.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
{{- end}}
})
{{- end }}
randomSuffix := acctest.RandString(t, 10)

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"random_suffix": randomSuffix,
"role": "{{ $.IamPolicy.AllowedIamRole }}",
{{- if $.IamPolicy.AdminIamRole }}
"admin_role": "{{ $.IamPolicy.AdminIamRole }}",
Expand All @@ -22,8 +24,8 @@
"project_id" : fmt.Sprintf("{{ $.IamPolicy.TestProjectName }}%s", acctest.RandString(t, 10)),
{{- end }}
{{- template "EnvVarContext" dict "TestEnvVars" $step.TestEnvVars "HasNewLine" true}}
{{- if $step.TestVarsOverrides }}
{{- range $varName, $override := $step.TestVarsOverrides }}
{{- if $step.TestContextVars }}
{{- range $varName, $override := $step.TestContextVars }}
"{{ $varName }}": {{ $override }},
{{- end }}
{{- end }}
Expand Down
4 changes: 2 additions & 2 deletions mmv1/templates/terraform/resource.html.markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ values will be stored in the raw state as plain text: {{ $.SensitivePropsToStrin

{{- if $.Samples }}
{{- range $sample := $.Samples }}
{{- range $step := $sample.Steps }}
{{- if not $step.ExcludeDocs }}
{{- range $index, $step := $sample.Steps }}
{{- if $step.ShouldGenerateDoc $index $sample }}
{{- if not (or $sample.ExcludeTest $step.TestEnvVars) }}
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="{{ $step.OiCSLink }}" target="_blank">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ resource "random_pet" "suffix" {
provider "google" {
region = "us-central1"
zone = "us-central1-c"
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ $.OicsHCLText -}}
{{ $.OicsHCLText -}}
12 changes: 1 addition & 11 deletions mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ func TestAcc{{ $s.TestSampleSlug $.Res.ProductMetadata.Name $.Res.Name }}(t *tes
})
{{- end }}

{{- $stepCount := len $s.TestSteps }}
{{- if gt $stepCount 1 }}
randomSuffix := acctest.RandString(t, 10)
{{- end }}

{{- range $i, $st := $s.TestSteps }}
{{- if eq $i 0 }}
Expand All @@ -100,17 +97,10 @@ func TestAcc{{ $s.TestSampleSlug $.Res.ProductMetadata.Name $.Res.Name }}(t *tes
context_{{ $i }} := map[string]interface{}{
{{- end }}
{{- template "EnvVarContext" dict "TestEnvVars" $st.TestEnvVars "HasNewLine" false}}
{{- range $varKey, $varVal := $st.Vars }}
"{{$varKey}}": {{ $varVal }},
{{- end }}
{{- range $varKey, $varVal := $st.TestVarsOverrides }}
{{- range $varKey, $varVal := $st.TestContextVars }}
"{{$varKey}}": {{ $varVal }},
{{- end }}
{{- if gt $stepCount 1 }}
"random_suffix": randomSuffix,
{{- else }}
"random_suffix": acctest.RandString(t, 10),
{{- end }}
}
{{- end }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ terraform destroy
```
```bash
yes
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "google_cloud_run_v2_service" "{{$.PrimaryResourceId}}" {
name = "{{index $.PrefixedVars "cloud_run_service_name"}}"
location = "us-central1"
deletion_protection = false
ingress = "INGRESS_TRAFFIC_ALL"
iap_enabled = {{index $.Vars "iap_enabled"}}
test_name = "{{index $.Vars "test_name"}}"
scaling {
max_instance_count = {{index $.Vars "max_instance_count"}}
}
template {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
2 changes: 1 addition & 1 deletion mmv1/templates/terraform/samples/static/motd.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ These examples use real resources that will be billed to the
Google Cloud Platform project you use - so make sure that you
run "terraform destroy" before quitting!

===
===
Loading
Loading