Skip to content

Commit

Permalink
fix: s3 categoryName (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Oct 19, 2023
1 parent b1b8ffe commit 95ca903
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetPackageName(str string) string {

// GetTFName returns the terraform name from the file content looking for comment //tfname: my_tfname.
func GetTFName(str string) (categoryName, resourceName string) {
reTFName := regexp.MustCompile(`^\/\/(?:\s+)?tfname:\s+([a-z]+)_?(.*)`)
reTFName := regexp.MustCompile(`^\/\/(?:\s+)?tfname:\s+([a-z0-9]+)_?(.*)`)

scanner := bufio.NewScanner(strings.NewReader(str))
for scanner.Scan() {
Expand Down
4 changes: 2 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func genTemplateConf(categoryName, resourceName, packageName, testDir, fileName,
Filename: fileName,
TestDir: testDir,
SchemaDir: schemaDir,
FullSnakeResourceName: strcase.ToSnake(categoryName + "_" + resourceName),
FullCamelResourceName: strcase.ToCamel(categoryName + "_" + resourceName),
FullSnakeResourceName: categoryName + "_" + strcase.ToSnake(resourceName),
FullCamelResourceName: categoryName + "_" + strcase.ToCamel(resourceName),
}

if resourceName == "" {
Expand Down
4 changes: 2 additions & 2 deletions templates/acc_test_datasource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var _ testsacc.TestACC = &{{ .FullCamelResourceName }}DataSource{}

const (
{{ .FullCamelResourceName }}DataSourceName = ResourceName("data.cloudavenue_{{ .FullSnakeResourceName }}")
{{ .FullCamelResourceName }}DataSourceName = testsacc.ResourceName("data.cloudavenue_{{ .FullSnakeResourceName }}")
)

type {{ .FullCamelResourceName }}DataSource struct{}
Expand All @@ -35,7 +35,7 @@ func (r *{{ .FullCamelResourceName }}DataSource) DependenciesConfig() (configs t
func (r *{{ .FullCamelResourceName }}DataSource) Tests(ctx context.Context) map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test {
return map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test{
// * Test One (example)
"example": func(_ context.Context, resourceName string) testsacc.Test {
"example": func(_ context.Context, _ string) testsacc.Test {
return testsacc.Test{
// ! Create testing
Create: testsacc.TFConfig{
Expand Down
2 changes: 1 addition & 1 deletion templates/acc_test_resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var _ testsacc.TestACC = &{{ .FullCamelResourceName }}Resource{}

const (
{{ .FullCamelResourceName }}ResourceName = ResourceName("cloudavenue_{{ .FullSnakeResourceName }}")
{{ .FullCamelResourceName }}ResourceName = testsacc.ResourceName("cloudavenue_{{ .FullSnakeResourceName }}")
)

type {{ .FullCamelResourceName }}Resource struct{}
Expand Down
18 changes: 14 additions & 4 deletions templates/datasource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (
"context"
"fmt"

{{ if eq .CategoryName "s3" }}
"github.com/aws/aws-sdk-go/service/s3"
{{ end }}

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
)

Expand All @@ -22,11 +27,14 @@ func New{{ .CamelName }}DataSource() datasource.DataSource {

type {{ .CamelName }}DataSource struct {
client *client.CloudAvenue

{{ if eq .CategoryName "s3" }}
s3Client *s3.S3
{{ else }}
// Uncomment the following lines if you need to access the resource's.
// org org.Org
// vdc vdc.VDC
// vapp vapp.VAPP
{{ end }}
}

// If the data source don't have same schema/structure as the resource, you can use the following code:
Expand All @@ -36,7 +44,9 @@ type {{ .CamelName }}DataSource struct {

// Init Initializes the data source.
func (d *{{ .CamelName }}DataSource) Init(ctx context.Context, dm *{{ .CamelName }}Model) (diags diag.Diagnostics) {

{{ if eq .CategoryName "s3" }}
d.s3Client = d.client.CAVSDK.V1.S3()
{{ else }}
// Uncomment the following lines if you need to access to the Org
// d.org, diags = org.Init(d.client)
// if diags.HasError() {
Expand All @@ -51,7 +61,7 @@ func (d *{{ .CamelName }}DataSource) Init(ctx context.Context, dm *{{ .CamelName

// Uncomment the following lines if you need to access to the VAPP
// d.vapp, diags = vapp.Init(d.client, d.vdc, dm.VAppID, dm.VAppName)

{{ end }}
return
}

Expand Down Expand Up @@ -81,7 +91,7 @@ func (d *{{ .CamelName }}DataSource) Configure(ctx context.Context, req datasour
}

func (d *{{ .CamelName }}DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_{{ .FullSnakeResourceName }}", r.client.GetOrgName(), metrics.Read)()
defer metrics.New("data.cloudavenue_{{ .FullSnakeResourceName }}", d.client.GetOrgName(), metrics.Read)()

config := &{{ .CamelName }}Model{}

Expand Down
16 changes: 13 additions & 3 deletions templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"fmt"

{{ if eq .CategoryName "s3" }}
"github.com/aws/aws-sdk-go/service/s3"
{{ end }}

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
)

Expand All @@ -28,11 +33,14 @@ func New{{ .CamelName }}Resource() resource.Resource {
// {{ .CamelName }}Resource is the resource implementation.
type {{ .CamelName }}Resource struct {
client *client.CloudAvenue

{{ if eq .CategoryName "s3" }}
s3Client *s3.S3
{{ else }}
// Uncomment the following lines if you need to access the resource's.
// org org.Org
// vdc vdc.VDC
// vapp vapp.VAPP
{{ end }}
}

// If the resource don't have same schema/structure as the data source, you can use the following code:
Expand All @@ -42,7 +50,9 @@ type {{ .CamelName }}Resource struct {

// Init Initializes the resource.
func (r *{{ .CamelName }}Resource) Init(ctx context.Context, rm *{{ .CamelName }}Model) (diags diag.Diagnostics) {

{{ if eq .CategoryName "s3" }}
r.s3Client = r.client.CAVSDK.V1.S3()
{{ else }}
// Uncomment the following lines if you need to access to the Org
// r.org, diags = org.Init(r.client)
// if diags.HasError() {
Expand All @@ -57,7 +67,7 @@ func (r *{{ .CamelName }}Resource) Init(ctx context.Context, rm *{{ .CamelName }

// Uncomment the following lines if you need to access to the VAPP
// r.vapp, diags = vapp.Init(r.client, r.vdc, rm.VAppID, rm.VAppName)

{{ end }}
return
}

Expand Down
6 changes: 4 additions & 2 deletions templates/unit_test_schema.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
// The fwresource import alias is so there is no collistion
// with the more typical acceptance testing import:
// "github.com/hashicorp/terraform-plugin-testing/helper/resource".
// The fwdatasource import alias is so there is no collistion
// fwdatasource "github.com/hashicorp/terraform-plugin-framework/datasource".
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/{{ .PackageName }}"
Expand Down Expand Up @@ -43,10 +45,10 @@ func Test{{ .CamelName }}DataSourceSchema(t *testing.T) {
t.Parallel()

ctx := context.Background()
schemaResponse := &fwresource.SchemaResponse{}
schemaResponse := &fwdatasource.SchemaResponse{}

// Instantiate the datasource.Datasource and call its Schema method
{{ .PackageName }}.New{{ .CamelName }}DataSource().Schema(ctx, fwresource.SchemaRequest{}, schemaResponse)
{{ .PackageName }}.New{{ .CamelName }}DataSource().Schema(ctx, fwdatasource.SchemaRequest{}, schemaResponse)

if schemaResponse.Diagnostics.HasError() {
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
Expand Down

0 comments on commit 95ca903

Please sign in to comment.