Skip to content

Commit

Permalink
minor changes to template
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-mazurkiewicz committed Oct 8, 2024
1 parent 5600066 commit 15270d8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ data "catalystcenter_template" "example" {
- `description` (String) Description
- `device_types` (Attributes List) List of device types (see [below for nested schema](#nestedatt--device_types))
- `language` (String) Language of the template
- `project_name` (String) ProjectName
- `software_type` (String) Software type
- `software_variant` (String) Software variant
- `software_version` (String) Software version
Expand All @@ -50,6 +49,7 @@ data "catalystcenter_template" "example" {

Read-Only:

- `composite` (Boolean) Is it composite template
- `id` (String) ID of the template
- `language` (String) Language of the template
- `name` (String) Name of the template
Expand Down
11 changes: 5 additions & 6 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ This resource can manage a Template.

```terraform
resource "catalystcenter_template" "example" {
project_id = "12345678-1234-1234-1234-123456789012"
name = "Template1"
project_name = "ProjectName"
description = "My description"
project_id = "12345678-1234-1234-1234-123456789012"
name = "Template1"
description = "My description"
device_types = [
{
product_family = "Switches and Hubs"
Expand Down Expand Up @@ -66,7 +65,6 @@ resource "catalystcenter_template" "example" {
- `composite` (Boolean) Is it composite template
- `containing_templates` (Attributes List) Containing templates for composite template (see [below for nested schema](#nestedatt--containing_templates))
- `description` (String) Description
- `project_name` (String) ProjectName
- `software_variant` (String) Software variant
- `software_version` (String) Software version
- `template_content` (String) Template content
Expand Down Expand Up @@ -94,14 +92,15 @@ Optional:

Required:

- `id` (String) ID of the template
- `language` (String) Language of the template
- Choices: `JINJA`, `VELOCITY`
- `name` (String) Name of the template
- `project_name` (String) Project name

Optional:

- `id` (String) ID of the template
- `composite` (Boolean) Is it composite template


<a id="nestedatt--template_params"></a>
Expand Down
7 changes: 3 additions & 4 deletions examples/resources/catalystcenter_template/resource.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
resource "catalystcenter_template" "example" {
project_id = "12345678-1234-1234-1234-123456789012"
name = "Template1"
project_name = "ProjectName"
description = "My description"
project_id = "12345678-1234-1234-1234-123456789012"
name = "Template1"
description = "My description"
device_types = [
{
product_family = "Switches and Hubs"
Expand Down
11 changes: 6 additions & 5 deletions gen/definitions/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ attributes:
data_source_query: true
description: Name of the template
example: Template1
- model_name: projectName
type: String
description: ProjectName
example: ProjectName
- model_name: description
type: String
description: Description
Expand Down Expand Up @@ -171,10 +167,11 @@ attributes:
attributes:
- model_name: name
type: String
id: true
mandatory: true
description: Name of the template
example: CompositeTemplate1
- model_name: id
id: true
type: String
description: ID of the template
example: 12345678-1234-1234-1234-123456789012
Expand All @@ -191,6 +188,10 @@ attributes:
mandatory: true
description: Language of the template
example: JINJA
- model_name: composite
type: Bool
description: Is it composite template
example: false
test_prerequisites: |
resource "catalystcenter_project" "test" {
name = "Project1"
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/data_source_catalystcenter_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ func (d *TemplateDataSource) Schema(ctx context.Context, req datasource.SchemaRe
Optional: true,
Computed: true,
},
"project_name": schema.StringAttribute{
MarkdownDescription: "ProjectName",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description",
Computed: true,
Expand Down Expand Up @@ -226,6 +222,10 @@ func (d *TemplateDataSource) Schema(ctx context.Context, req datasource.SchemaRe
MarkdownDescription: "Language of the template",
Computed: true,
},
"composite": schema.BoolAttribute{
MarkdownDescription: "Is it composite template",
Computed: true,
},
},
},
},
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/data_source_catalystcenter_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
func TestAccDataSourceCcTemplate(t *testing.T) {
var checks []resource.TestCheckFunc
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_template.test", "name", "Template1"))
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_template.test", "project_name", "ProjectName"))
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_template.test", "description", "My description"))
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_template.test", "device_types.0.product_family", "Switches and Hubs"))
checks = append(checks, resource.TestCheckResourceAttr("data.catalystcenter_template.test", "device_types.0.product_series", "Cisco Catalyst 9300 Series Switches"))
Expand Down Expand Up @@ -80,7 +79,6 @@ func testAccDataSourceCcTemplateConfig() string {
config := `resource "catalystcenter_template" "test" {` + "\n"
config += ` project_id = catalystcenter_project.test.id` + "\n"
config += ` name = "Template1"` + "\n"
config += ` project_name = "ProjectName"` + "\n"
config += ` description = "My description"` + "\n"
config += ` device_types = [{` + "\n"
config += ` product_family = "Switches and Hubs"` + "\n"
Expand Down
35 changes: 16 additions & 19 deletions internal/provider/model_catalystcenter_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Template struct {
Id types.String `tfsdk:"id"`
ProjectId types.String `tfsdk:"project_id"`
Name types.String `tfsdk:"name"`
ProjectName types.String `tfsdk:"project_name"`
Description types.String `tfsdk:"description"`
DeviceTypes []TemplateDeviceTypes `tfsdk:"device_types"`
Language types.String `tfsdk:"language"`
Expand Down Expand Up @@ -78,6 +77,7 @@ type TemplateContainingTemplates struct {
Id types.String `tfsdk:"id"`
ProjectName types.String `tfsdk:"project_name"`
Language types.String `tfsdk:"language"`
Composite types.Bool `tfsdk:"composite"`
}

type TemplateTemplateParamsRanges struct {
Expand Down Expand Up @@ -114,9 +114,6 @@ func (data Template) toBody(ctx context.Context, state Template) string {
if !data.Name.IsNull() {
body, _ = sjson.Set(body, "name", data.Name.ValueString())
}
if !data.ProjectName.IsNull() {
body, _ = sjson.Set(body, "projectName", data.ProjectName.ValueString())
}
if !data.Description.IsNull() {
body, _ = sjson.Set(body, "description", data.Description.ValueString())
}
Expand Down Expand Up @@ -233,6 +230,9 @@ func (data Template) toBody(ctx context.Context, state Template) string {
if !item.Language.IsNull() {
itemBody, _ = sjson.Set(itemBody, "language", item.Language.ValueString())
}
if !item.Composite.IsNull() {
itemBody, _ = sjson.Set(itemBody, "composite", item.Composite.ValueBool())
}
body, _ = sjson.SetRaw(body, "containingTemplates.-1", itemBody)
}
}
Expand All @@ -248,11 +248,6 @@ func (data *Template) fromBody(ctx context.Context, res gjson.Result) {
} else {
data.Name = types.StringNull()
}
if value := res.Get("projectName"); value.Exists() {
data.ProjectName = types.StringValue(value.String())
} else {
data.ProjectName = types.StringNull()
}
if value := res.Get("description"); value.Exists() {
data.Description = types.StringValue(value.String())
} else {
Expand Down Expand Up @@ -426,6 +421,11 @@ func (data *Template) fromBody(ctx context.Context, res gjson.Result) {
} else {
item.Language = types.StringNull()
}
if cValue := v.Get("composite"); cValue.Exists() {
item.Composite = types.BoolValue(cValue.Bool())
} else {
item.Composite = types.BoolNull()
}
data.ContainingTemplates = append(data.ContainingTemplates, item)
return true
})
Expand All @@ -441,11 +441,6 @@ func (data *Template) updateFromBody(ctx context.Context, res gjson.Result) {
} else {
data.Name = types.StringNull()
}
if value := res.Get("projectName"); value.Exists() && !data.ProjectName.IsNull() {
data.ProjectName = types.StringValue(value.String())
} else {
data.ProjectName = types.StringNull()
}
if value := res.Get("description"); value.Exists() && !data.Description.IsNull() {
data.Description = types.StringValue(value.String())
} else {
Expand Down Expand Up @@ -644,8 +639,8 @@ func (data *Template) updateFromBody(ctx context.Context, res gjson.Result) {
data.Composite = types.BoolNull()
}
for i := range data.ContainingTemplates {
keys := [...]string{"name"}
keyValues := [...]string{data.ContainingTemplates[i].Name.ValueString()}
keys := [...]string{"id"}
keyValues := [...]string{data.ContainingTemplates[i].Id.ValueString()}

var r gjson.Result
res.Get("containingTemplates").ForEach(
Expand Down Expand Up @@ -686,6 +681,11 @@ func (data *Template) updateFromBody(ctx context.Context, res gjson.Result) {
} else {
data.ContainingTemplates[i].Language = types.StringNull()
}
if value := r.Get("composite"); value.Exists() && !data.ContainingTemplates[i].Composite.IsNull() {
data.ContainingTemplates[i].Composite = types.BoolValue(value.Bool())
} else {
data.ContainingTemplates[i].Composite = types.BoolNull()
}
}
}

Expand All @@ -696,9 +696,6 @@ func (data *Template) isNull(ctx context.Context, res gjson.Result) bool {
if !data.Name.IsNull() {
return false
}
if !data.ProjectName.IsNull() {
return false
}
if !data.Description.IsNull() {
return false
}
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/resource_catalystcenter_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
MarkdownDescription: helpers.NewAttributeDescription("Name of the template").String,
Required: true,
},
"project_name": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("ProjectName").String,
Optional: true,
},
"description": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Description").String,
Optional: true,
Expand Down Expand Up @@ -232,7 +228,7 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
},
"id": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("ID of the template").String,
Optional: true,
Required: true,
},
"project_name": schema.StringAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Project name").String,
Expand All @@ -245,6 +241,10 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
stringvalidator.OneOf("JINJA", "VELOCITY"),
},
},
"composite": schema.BoolAttribute{
MarkdownDescription: helpers.NewAttributeDescription("Is it composite template").String,
Optional: true,
},
},
},
},
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/resource_catalystcenter_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
func TestAccCcTemplate(t *testing.T) {
var checks []resource.TestCheckFunc
checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_template.test", "name", "Template1"))
checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_template.test", "project_name", "ProjectName"))
checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_template.test", "description", "My description"))
checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_template.test", "device_types.0.product_family", "Switches and Hubs"))
checks = append(checks, resource.TestCheckResourceAttr("catalystcenter_template.test", "device_types.0.product_series", "Cisco Catalyst 9300 Series Switches"))
Expand Down Expand Up @@ -104,7 +103,6 @@ func testAccCcTemplateConfig_all() string {
config := `resource "catalystcenter_template" "test" {` + "\n"
config += ` project_id = catalystcenter_project.test.id` + "\n"
config += ` name = "Template1"` + "\n"
config += ` project_name = "ProjectName"` + "\n"
config += ` description = "My description"` + "\n"
config += ` device_types = [{` + "\n"
config += ` product_family = "Switches and Hubs"` + "\n"
Expand Down

0 comments on commit 15270d8

Please sign in to comment.