Skip to content

Commit

Permalink
Step 1: update the generated Go client code (#81)
Browse files Browse the repository at this point in the history
* Step 1: update the generated Go client code

* TF doc update

* CI Go update, prep for actual end user ReadMe, add project name pattern field in doc and tests

* Debugging a deserialization issue with parameters. . .

* Revert to an older version of the openapitools/openapi-generator-cli image

* unindent a test file
  • Loading branch information
matthewcummings authored Dec 11, 2023
1 parent 8befaac commit f52b74f
Show file tree
Hide file tree
Showing 122 changed files with 24,287 additions and 6,861 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.21.5'
go-version-file: 'go.mod'
cache: true
-
Expand Down
27 changes: 14 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.21.0'
go-version: '>=1.21.5'
check-latest: true
cache: false
id: go
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
steps:
- uses: actions/setup-go@v5
with:
go-version: '>=1.21.0'
go-version: '>=1.21.5'
check-latest: true
cache: false
- uses: actions/checkout@v4
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.21.0'
go-version: '>=1.21.5'
check-latest: true
cache: false
id: go
Expand Down Expand Up @@ -102,13 +102,14 @@ jobs:
run: |
go test -timeout 60m -v -cover ./cloudtruth
- name: TF acceptance tests (prod backend)
timeout-minutes: 60
env:
TF_ACC: "1" # Enables provider acceptance test mode
TF_VAR_cloudtruth_api_key: ${{ secrets.ACCEPTANCE_TEST_TOKEN }}
CLOUDTRUTH_API_KEY: ${{ secrets.ACCEPTANCE_TEST_TOKEN }} # for tests without provider defs
CLOUDTRUTH_PROJECT: AcceptanceTest
CLOUDTRUTH_ENVIRONMENT: default
run: |
go test -timeout 60m -v -cover ./cloudtruth
# DISABLED FOR NOW, RE-ENABLE WHEN DB CLEANUP IS DONE
# - name: TF acceptance tests (prod backend)
# timeout-minutes: 60
# env:
# TF_ACC: "1" # Enables provider acceptance test mode
# TF_VAR_cloudtruth_api_key: ${{ secrets.ACCEPTANCE_TEST_TOKEN }}
# CLOUDTRUTH_API_KEY: ${{ secrets.ACCEPTANCE_TEST_TOKEN }} # for tests without provider defs
# CLOUDTRUTH_PROJECT: AcceptanceTest
# CLOUDTRUTH_ENVIRONMENT: default
# run: |
# go test -timeout 60m -v -cover ./cloudtruth
8 changes: 6 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,19 @@ pkg/cloudtruth/client.go: pkg/openapi.yml
docker run --rm \
-v "$(shell pwd)/pkg:/pkg" \
--user "$(shell id -u):$(shell id -g)" \
openapitools/openapi-generator-cli generate \
openapitools/openapi-generator-cli:v7.0.0 generate \
-i /pkg/openapi.yml \
-g go \
-o /pkg/cloudtruthapi \
--additional-properties packageName=cloudtruthapi \
--additional-properties packageVersion=1.0.0 \
--additional-properties enumClassPrefix=true \
--type-mappings=object=interface{}
rm pkg/cloudtruthapi/go.mod pkg/cloudtruthapi/go.sum # These files are unecessary and break local imports
# These files are unecessary and break local imports
rm pkg/cloudtruthapi/go.mod pkg/cloudtruthapi/go.sum
# These files are unecessary
rm -rf pkg/cloudtruthapi/test pkg/cloudtruthapi/api pkg/cloudtruthapi/.openapi-generator pkg/cloudtruthapi/docs
rm pkg/cloudtruthapi/.travis.yml pkg/cloudtruthapi/git_push.sh pkg/cloudtruthapi/README.md

pkg/openapi.yml: pkg
curl -s https://api.cloudtruth.io/api/schema/ > pkg/openapi.yml
Expand Down
54 changes: 0 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,3 @@

This is the official Terraform provider for [CloudTruth](https://cloudtruth.com/)

## Requirements

- [Terraform](https://www.terraform.io/downloads.html) >= 0.15.x
- [Go](https://golang.org/doc/install) >= 1.20

## Building The Provider

1. Clone the repository
1. Enter the repository directory
1. Build the provider using the Go `install` command:
```sh
$ go install
```

## Adding Dependencies

This provider uses [Go modules](https://github.com/golang/go/wiki/Modules).
Please see the Go documentation for the most up to date information about using Go modules.

To add a new dependency `github.com/author/dependency` to your Terraform provider:

```
go get github.com/author/dependency
go mod tidy
```

Then commit the changes to `go.mod` and `go.sum`.

## Developing the Provider

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).

To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.

To generate or update documentation, run `go generate`.

In order to run the full suite of Acceptance tests, run `make testacc`.

*Note:* Acceptance tests create real resources, and often cost money to run.

If you run the tests in a fork of this repo, you will need to recreate the boilerplate CloudTruth resources which exist in the dedicated
acceptance test account. We will provide a bootstrapping script to ensure that these resource exist in your account.

Additionally, you will need to specify the target CloudTruth project and environment(s) as well as your CloudTruth API key. You can
specify the API key with a `TF_VAR_cloudtruth_api_key` environment variable or with a `CLOUDTRUTH_API_KEY` environment variable (a la the CloudTruth
CLI). You can specify the the project and/or environments inline in your HCL files or via the `CLOUDTRUTH_PROJECT` and `CLOUDTRUTH_ENVIRONMENT`
environment variables.

```sh
$ make testacc
```

### Commit hooks
To enable the commit hooks stored in `.githooks` in your local repo, run `git config --local core.hooksPath .githooks/`
4 changes: 2 additions & 2 deletions cloudtruth/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
envID := d.Id()
envName := d.Get("name").(string)
envDesc := d.Get("description").(string)
patchedEnv := cloudtruthapi.PatchedEnvironment{}
patchedEnv := cloudtruthapi.PatchedEnvironmentUpdate{}
hasChange := false
if d.HasChange("name") {
patchedEnv.SetName(envName)
Expand All @@ -167,7 +167,7 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
var r *http.Response
var err error
_, r, err = c.openAPIClient.EnvironmentsAPI.EnvironmentsPartialUpdate(ctx,
envID).PatchedEnvironment(patchedEnv).Execute()
envID).PatchedEnvironmentUpdate(patchedEnv).Execute()
if err != nil {
return handleAPIError(fmt.Sprintf("resourceEnvironmentUpdate: error updating environment %s", envName), r, err)
}
Expand Down
8 changes: 4 additions & 4 deletions cloudtruth/resource_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func updateParameter(ctx context.Context, paramID, projID string, d *schema.Reso
c *cloudTruthClient) (*http.Response, error) {
tflog.Debug(ctx, "entering updateParameter")
defer tflog.Debug(ctx, "exiting updateParameter")
patchedParam := cloudtruthapi.PatchedParameter{}
patchedParam := cloudtruthapi.PatchedParameterUpdate{}
hasParamChange := false
if d.HasChange("name") {
paramName := d.Get("name").(string)
Expand All @@ -341,7 +341,7 @@ func updateParameter(ctx context.Context, paramID, projID string, d *schema.Reso
if hasParamChange {
// The caller handles retries
_, r, err = c.openAPIClient.ProjectsAPI.ProjectsParametersPartialUpdate(ctx, paramID, projID).
PatchedParameter(patchedParam).Execute()
PatchedParameterUpdate(patchedParam).Execute()
if err != nil {
return r, err
}
Expand All @@ -367,7 +367,7 @@ func updateParameterRule(ctx context.Context, paramID, projID, ruleName, ruleID,
if err != nil {
return nil, err
}
paramRule := cloudtruthapi.NewParameterRuleWithDefaults()
paramRule := cloudtruthapi.NewParameterRuleUpdateWithDefaults()
paramRule.SetId(ruleID)
paramRule.SetConstraint(ruleVal)
paramRule.SetType(*ruleType)
Expand All @@ -376,7 +376,7 @@ func updateParameterRule(ctx context.Context, paramID, projID, ruleName, ruleID,

var r *http.Response
for retryCount < ruleOperationRetries {
ruleUpdateRequest = c.openAPIClient.ProjectsAPI.ProjectsParametersRulesUpdate(ctx, ruleID, paramID, projID).ParameterRule(*paramRule)
ruleUpdateRequest = c.openAPIClient.ProjectsAPI.ProjectsParametersRulesUpdate(ctx, ruleID, paramID, projID).ParameterRuleUpdate(*paramRule)
_, r, err = ruleUpdateRequest.Execute()
if r.StatusCode >= 500 {
tflog.Debug(ctx, fmt.Sprintf("updateParameterRule: error updating rule %s: %+v", ruleName, err))
Expand Down
4 changes: 2 additions & 2 deletions cloudtruth/resource_parameter_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func resourceParameterValueRead(ctx context.Context, d *schema.ResourceData, met
}

func updateParameterValue(ctx context.Context, paramID, paramValueID, projID string, d *schema.ResourceData, c *cloudTruthClient) (*http.Response, error) {
updateValue := cloudtruthapi.NewValueWithDefaults()
updateValue := cloudtruthapi.NewValueUpdateWithDefaults()
hasParamValueChange := false
if d.HasChange("value") {
value := d.Get("value").(string)
Expand Down Expand Up @@ -309,7 +309,7 @@ func updateParameterValue(ctx context.Context, paramID, paramValueID, projID str
if hasParamValueChange {
// No retry logic here, the caller handles that
_, r, err = c.openAPIClient.ProjectsAPI.ProjectsParametersValuesUpdate(ctx, paramValueID, paramID,
projID).Value(*updateValue).Execute()
projID).ValueUpdate(*updateValue).Execute()
if err != nil {
return r, err
}
Expand Down
14 changes: 12 additions & 2 deletions cloudtruth/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func resourceProject() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"parameter_name_pattern": {
Description: "A regular expression parameter names must match",
Type: schema.TypeString,
Optional: true,
Default: "",
},
"description": {
Description: "Description of the project",
Type: schema.TypeString,
Expand All @@ -51,11 +57,15 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, meta any
defer tflog.Debug(ctx, "exiting resourceProjectCreate")
c := meta.(*cloudTruthClient)
projectName := d.Get("name").(string)
//parameterNamePattern := d.Get("parameter_name_pattern").(string)
projectDesc := d.Get("description").(string)
projectCreate := cloudtruthapi.NewProjectCreate(projectName)
if projectDesc != "" {
projectCreate.SetDescription(projectDesc)
}
/*if parameterNamePattern != "" {
projectCreate.
}*/
projectParent := d.Get("parent").(string)
if projectParent != "" {
parent, err := c.getProjectURL(ctx, projectParent)
Expand Down Expand Up @@ -149,7 +159,7 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, meta any
projectName := d.Get("name").(string)
projectDesc := d.Get("description").(string)

patchedProject := cloudtruthapi.PatchedProject{}
patchedProject := cloudtruthapi.PatchedProjectUpdate{}
hasChange := false

// explicitly not checking for/allowing reparenting from the provider because the UI does not allow it
Expand All @@ -167,7 +177,7 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, meta any
var r *http.Response
var err error
_, r, err = c.openAPIClient.ProjectsAPI.ProjectsPartialUpdate(ctx,
projectID).PatchedProject(patchedProject).Execute()
projectID).PatchedProjectUpdate(patchedProject).Execute()
if err != nil {
return handleAPIError(fmt.Sprintf("resourceProjectUpdate: error updating project %s", projectName), r, err)
}
Expand Down
24 changes: 12 additions & 12 deletions cloudtruth/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

const desc = "Just a description of a project"
const updateDesc = "A new description of a project"
const parameterNamePattern = "foo-*"
const updateParameterNamePattern = "foobar-*"

func TestAccResourceProjectBasic(t *testing.T) {
createProjName := fmt.Sprintf("TestProject-%s", uuid.New().String())
Expand All @@ -20,23 +22,19 @@ func TestAccResourceProjectBasic(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: testAccResourceProjectCreateBasic(createProjName, desc),
Config: testAccResourceProjectCreateBasic(createProjName, desc, parameterNamePattern),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", createProjName),
resource.TestCheckResourceAttr(resourceName, "description", desc),
resource.TestCheckResourceAttr(resourceName, "parameter_name_pattern", parameterNamePattern),
),
},
/* This is leading to occasional timing issues which appear to be only affecting the tests
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},*/
{
Config: testAccResourceProjectUpdateBasic(updateProjName, updateDesc),
Config: testAccResourceProjectUpdateBasic(updateProjName, updateDesc, updateParameterNamePattern),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", updateProjName),
resource.TestCheckResourceAttr(resourceName, "description", updateDesc),
resource.TestCheckResourceAttr(resourceName, "parameter_name_pattern", updateParameterNamePattern),
),
},
},
Expand All @@ -63,22 +61,24 @@ func TestAccResourceProjectNested(t *testing.T) {
})
}

func testAccResourceProjectCreateBasic(projName, desc string) string {
func testAccResourceProjectCreateBasic(projName, desc, paramNamePattern string) string {
return fmt.Sprintf(`
resource "cloudtruth_project" "basic" {
name = "%s"
description = "%s"
parameter_name_pattern = "%s"
}
`, projName, desc)
`, projName, desc, paramNamePattern)
}

func testAccResourceProjectUpdateBasic(projName, desc string) string {
func testAccResourceProjectUpdateBasic(projName, desc, paramNamePattern string) string {
return fmt.Sprintf(`
resource "cloudtruth_project" "basic" {
name = "%s"
description = "%s"
parameter_name_pattern = "%s"
}
`, projName, desc)
`, projName, desc, paramNamePattern)
}

func testAccResourceProjectNested(projectOne, projectTwo, projectThree string) string {
Expand Down
4 changes: 2 additions & 2 deletions cloudtruth/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta an
templateDesc := d.Get("description").(string)
templateID := d.Id()

patchedTemplate := cloudtruthapi.PatchedTemplate{}
patchedTemplate := cloudtruthapi.PatchedTemplateUpdate{}
hasChange := false
if d.HasChange("name") {
patchedTemplate.SetName(templateName)
Expand All @@ -165,7 +165,7 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta an
var r *http.Response
var err error
_, r, err = c.openAPIClient.ProjectsAPI.ProjectsTemplatesPartialUpdate(ctx, templateID,
*projID).PatchedTemplate(patchedTemplate).Execute()
*projID).PatchedTemplateUpdate(patchedTemplate).Execute()
if err != nil {
return handleAPIError(fmt.Sprintf("resourceTemplateUpdate: error updating template %s", templateName), r, err)
}
Expand Down
17 changes: 8 additions & 9 deletions cloudtruth/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ func resourceTypeCreate(ctx context.Context, d *schema.ResourceData, meta any) d
defer tflog.Debug(ctx, "exiting resourceTypeCreate")
c := meta.(*cloudTruthClient)
typeName := d.Get("name").(string)
typeCreate := cloudtruthapi.NewParameterTypeCreate(typeName)
// concreteType is the underlying type (builtin or custom) of the cloudtruth_type resource
concreteType := c.lookupType(ctx, d.Get("type").(string))
baseParamType := getBaseParamType(ctx, concreteType, c)
typeCreate := cloudtruthapi.NewParameterTypeCreate(typeName, baseParamType)
typeDesc := d.Get("description").(string)
if typeDesc != "" {
typeCreate.SetDescription(typeDesc)
}
// concreteType is the underlying type (builtin or custom) of the cloudtruth_type resource
concreteType := c.lookupType(ctx, d.Get("type").(string))
baseParamType := getBaseParamType(ctx, concreteType, c)

var rules map[string]any
var err error
if rules, err = validateAndFetchRules(ctx, c, d, concreteType); err != nil {
Expand Down Expand Up @@ -282,14 +281,14 @@ func updateTypeRule(ctx context.Context, paramID, ruleName, ruleID, baseParamTyp
if err != nil {
return nil, err
}
paramTypeRule := cloudtruthapi.NewParameterTypeRuleWithDefaults()
paramTypeRule := cloudtruthapi.NewParameterTypeRuleUpdateWithDefaults()
paramTypeRule.SetId(ruleID)
paramTypeRule.SetConstraint(ruleVal)
paramTypeRule.SetType(*ruleType)

var r *http.Response
for retryCount < ruleOperationRetries {
ruleUpdateRequest = c.openAPIClient.TypesAPI.TypesRulesUpdate(ctx, ruleID, paramID).ParameterTypeRule(*paramTypeRule)
ruleUpdateRequest = c.openAPIClient.TypesAPI.TypesRulesUpdate(ctx, ruleID, paramID).ParameterTypeRuleUpdate(*paramTypeRule)
_, r, err = ruleUpdateRequest.Execute()
if r.StatusCode >= 500 {
tflog.Debug(ctx, fmt.Sprintf("updateTypeRule: error updating rule %s: %+v", ruleName, err))
Expand Down Expand Up @@ -338,7 +337,7 @@ func resourceTypeUpdate(ctx context.Context, d *schema.ResourceData, meta any) d
c := meta.(*cloudTruthClient)
typeName := d.Get("name").(string)
hasChange := false
patchedTypeUpdate := cloudtruthapi.NewPatchedParameterType()
patchedTypeUpdate := cloudtruthapi.NewPatchedParameterTypeUpdate()
if d.HasChange("description") {
typeDesc := d.Get("description").(string)
patchedTypeUpdate.SetDescription(typeDesc)
Expand All @@ -350,7 +349,7 @@ func resourceTypeUpdate(ctx context.Context, d *schema.ResourceData, meta any) d
retryError := retry.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate), func() *retry.RetryError {
var r *http.Response
var err error
_, r, err = c.openAPIClient.TypesAPI.TypesPartialUpdate(ctx, typeID).PatchedParameterType(*patchedTypeUpdate).Execute()
_, r, err = c.openAPIClient.TypesAPI.TypesPartialUpdate(ctx, typeID).PatchedParameterTypeUpdate(*patchedTypeUpdate).Execute()
if err != nil {
return handleAPIError(fmt.Sprintf("resourceTypeUpdate: error updating type %s", typeName), r, err)
}
Expand Down
Loading

0 comments on commit f52b74f

Please sign in to comment.