Skip to content

Commit

Permalink
chore: factorize apps schema
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 committed Nov 5, 2024
1 parent f9ba81b commit e0d5cf0
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 300 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.57
version: v1.61
...
6 changes: 3 additions & 3 deletions docs/resources/addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ List of available providers:

### Required

- `name` (String) Name of the addon
- `plan` (String) billing plan
- `name` (String) Name of the service
- `plan` (String) Database size and spec
- `third_party_provider` (String) Provider ID

### Optional

- `region` (String) Geographical region where the addon will be deployed (when relevant)
- `region` (String) Geographical region where the data will be stored

### Read-Only

Expand Down
18 changes: 9 additions & 9 deletions docs/resources/java_war.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
page_title: "clevercloud_java_war Resource - terraform-provider-clevercloud"
subcategory: ""
description: |-
Manage Java https://www.java.com/en/ applications.
Manage Java applications.
See Java product https://www.clever-cloud.com/doc/getting-started/by-language/java/ specification.
Example usage
Basic
terraform
resource "clevercloud_java_war" "myapp" {
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
}
Advanced
terraform
resource "clevercloud_java_war" "myapp" {
name = "tf-myapp"
region = "par"
Expand Down
16 changes: 8 additions & 8 deletions docs/resources/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ description: |-
See NodeJS product https://www.clever-cloud.com/nodejs-hosting/ specification.
Example usage
Basic
terraform
resource "clevercloud_nodejs" "myapp" {
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
}
Advanced
terraform
resource "clevercloud_nodejs" "myapp" {
name = "tf-myapp"
region = "par"
Expand Down
16 changes: 8 additions & 8 deletions docs/resources/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ description: |-
See Python product https://www.clever-cloud.com/python-hosting/ specification.
Example usage
Basic
terraform
resource "clevercloud_python" "myapp" {
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
}
Advanced
terraform
resource "clevercloud_python" "myapp" {
name = "tf-myapp"
region = "par"
Expand Down
18 changes: 9 additions & 9 deletions docs/resources/scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
page_title: "clevercloud_scala Resource - terraform-provider-clevercloud"
subcategory: ""
description: |-
Manage Scala https://www.scala-lang.org/ applications.
Manage Scala applications.
See Scala product https://www.clever-cloud.com/scala-hosting/ specification.
Example usage
Basic
terraform
resource "clevercloud_scala" "myapp" {
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
}
Advanced
terraform
resource "clevercloud_scala" "myapp" {
name = "tf-myapp"
region = "par"
Expand Down
16 changes: 8 additions & 8 deletions docs/resources/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ description: |-
See Static product https://www.clever-cloud.com/doc/deploy/application/static/static/ specification.
Example usage
Basic
terraform
resource "clevercloud_static" "myapp" {
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
name = "tf-myapp"
region = "par"
min_instance_count = 1
max_instance_count = 2
smallest_flavor = "XS"
biggest_flavor = "M"
}
Advanced
terraform
resource "clevercloud_static" "myapp" {
name = "tf-myapp"
region = "par"
Expand Down
33 changes: 33 additions & 0 deletions pkg/attributes/addon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package attributes

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"go.clever-cloud.com/terraform-provider/pkg"
)

type Addon struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Plan types.String `tfsdk:"plan"`
Region types.String `tfsdk:"region"`
CreationDate types.Int64 `tfsdk:"creation_date"`
}

var addonCommon = map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier"},
"name": schema.StringAttribute{Required: true, MarkdownDescription: "Name of the service"},
"plan": schema.StringAttribute{Required: true, MarkdownDescription: "Database size and spec"},
"region": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString("par"),
MarkdownDescription: "Geographical region where the data will be stored",
},
"creation_date": schema.Int64Attribute{Computed: true, MarkdownDescription: "Date of database creation"},
}

func WithAddonCommons(runtimeSpecifics map[string]schema.Attribute) map[string]schema.Attribute {
return pkg.Merge(addonCommon, runtimeSpecifics)
}
36 changes: 25 additions & 11 deletions pkg/attributes/common.go → pkg/attributes/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ import (
"go.clever-cloud.com/terraform-provider/pkg"
)

type Runtime struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
MinInstanceCount types.Int64 `tfsdk:"min_instance_count"`
MaxInstanceCount types.Int64 `tfsdk:"max_instance_count"`
SmallestFlavor types.String `tfsdk:"smallest_flavor"`
BiggestFlavor types.String `tfsdk:"biggest_flavor"`
BuildFlavor types.String `tfsdk:"build_flavor"`
Region types.String `tfsdk:"region"`
StickySessions types.Bool `tfsdk:"sticky_sessions"`
RedirectHTTPS types.Bool `tfsdk:"redirect_https"`
VHost types.String `tfsdk:"vhost"`
AdditionalVHosts types.List `tfsdk:"additional_vhosts"`
DeployURL types.String `tfsdk:"deploy_url"`
Dependencies types.Set `tfsdk:"dependencies"`
Deployment *Deployment `tfsdk:"deployment"`
Hooks *Hooks `tfsdk:"hooks"`

// Env
AppFolder types.String `tfsdk:"app_folder"`
Environment types.Map `tfsdk:"environment"`
}

// This attributes are used on several runtimes
var runtimeCommon = map[string]schema.Attribute{
// client provided
Expand Down Expand Up @@ -124,15 +148,5 @@ var runtimeCommon = map[string]schema.Attribute{
}

func WithRuntimeCommons(runtimeSpecifics map[string]schema.Attribute) map[string]schema.Attribute {
m := map[string]schema.Attribute{}

for attrName, attr := range runtimeCommon {
m[attrName] = attr
}

for attrName, attr := range runtimeSpecifics {
m[attrName] = attr
}

return m
return pkg.Merge(runtimeCommon, runtimeSpecifics)
}
24 changes: 4 additions & 20 deletions pkg/resources/addon/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import (

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
"go.clever-cloud.com/terraform-provider/pkg/attributes"
)

type Addon struct {
ID types.String `tfsdk:"id"`
attributes.Addon
ThirdPartyProvider types.String `tfsdk:"third_party_provider"`
Name types.String `tfsdk:"name"`
CreationDate types.Int64 `tfsdk:"creation_date"`
Plan types.String `tfsdk:"plan"`
Region types.String `tfsdk:"region"`
Configurations types.Map `tfsdk:"configurations"`
}

Expand All @@ -27,28 +23,16 @@ func (r ResourceAddon) Schema(_ context.Context, req resource.SchemaRequest, res
resp.Schema = schema.Schema{
Version: 0,
MarkdownDescription: resourcePostgresqlDoc,
Attributes: map[string]schema.Attribute{
// customer provided
"name": schema.StringAttribute{Required: true, MarkdownDescription: "Name of the addon"},
"plan": schema.StringAttribute{Required: true, MarkdownDescription: "billing plan"},
"region": schema.StringAttribute{
Optional: true,
Computed: true,
Default: stringdefault.StaticString("par"),
MarkdownDescription: "Geographical region where the addon will be deployed (when relevant)",
},
Attributes: attributes.WithAddonCommons(map[string]schema.Attribute{
"third_party_provider": schema.StringAttribute{Required: true, MarkdownDescription: "Provider ID"},

// provider
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier"},
"creation_date": schema.Int64Attribute{Computed: true, MarkdownDescription: "Date of database creation"},
"configurations": schema.MapAttribute{
Computed: true,
Sensitive: true,
MarkdownDescription: "Any configuration exposed by the addon",
ElementType: types.StringType,
},
},
}),
}
}

Expand Down
24 changes: 1 addition & 23 deletions pkg/resources/docker/resource_docker_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,7 @@ import (
)

type Docker struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
MinInstanceCount types.Int64 `tfsdk:"min_instance_count"`
MaxInstanceCount types.Int64 `tfsdk:"max_instance_count"`
SmallestFlavor types.String `tfsdk:"smallest_flavor"`
BiggestFlavor types.String `tfsdk:"biggest_flavor"`
BuildFlavor types.String `tfsdk:"build_flavor"`
Region types.String `tfsdk:"region"`
StickySessions types.Bool `tfsdk:"sticky_sessions"`
RedirectHTTPS types.Bool `tfsdk:"redirect_https"`
VHost types.String `tfsdk:"vhost"`
AdditionalVHosts types.List `tfsdk:"additional_vhosts"`
DeployURL types.String `tfsdk:"deploy_url"`
Deployment *attributes.Deployment `tfsdk:"deployment"`
Hooks *attributes.Hooks `tfsdk:"hooks"`
Dependencies types.Set `tfsdk:"dependencies"`

// Env
AppFolder types.String `tfsdk:"app_folder"`
Environment types.Map `tfsdk:"environment"`

// Docker related
attributes.Runtime
Dockerfile types.String `tfsdk:"dockerfile"`
ContainerPort types.Int64 `tfsdk:"container_port"`
ContainerPortTCP types.Int64 `tfsdk:"container_port_tcp"`
Expand Down
24 changes: 1 addition & 23 deletions pkg/resources/java/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,7 @@ import (
)

type Java struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
MinInstanceCount types.Int64 `tfsdk:"min_instance_count"`
MaxInstanceCount types.Int64 `tfsdk:"max_instance_count"`
SmallestFlavor types.String `tfsdk:"smallest_flavor"`
BiggestFlavor types.String `tfsdk:"biggest_flavor"`
BuildFlavor types.String `tfsdk:"build_flavor"`
Region types.String `tfsdk:"region"`
StickySessions types.Bool `tfsdk:"sticky_sessions"`
RedirectHTTPS types.Bool `tfsdk:"redirect_https"`
VHost types.String `tfsdk:"vhost"`
AdditionalVHosts types.List `tfsdk:"additional_vhosts"`
DeployURL types.String `tfsdk:"deploy_url"`
Deployment *attributes.Deployment `tfsdk:"deployment"`
Hooks *attributes.Hooks `tfsdk:"hooks"`
Dependencies types.Set `tfsdk:"dependencies"`

// Env
AppFolder types.String `tfsdk:"app_folder"`
Environment types.Map `tfsdk:"environment"`

// Java related
attributes.Runtime
JavaVersion types.String `tfsdk:"java_version"`
}

Expand Down
Loading

0 comments on commit e0d5cf0

Please sign in to comment.