-
Notifications
You must be signed in to change notification settings - Fork 2
/
freeform_config_template.go
121 lines (114 loc) · 4.72 KB
/
freeform_config_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package blueprint
import (
"context"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"regexp"
)
type FreeformConfigTemplate struct {
Id types.String `tfsdk:"id"`
BlueprintId types.String `tfsdk:"blueprint_id"`
Name types.String `tfsdk:"name"`
Text types.String `tfsdk:"text"`
Tags types.Set `tfsdk:"tags"`
}
func (o FreeformConfigTemplate) DataSourceAttributes() map[string]dataSourceSchema.Attribute {
return map[string]dataSourceSchema.Attribute{
"blueprint_id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID. Used to identify " +
"the Blueprint where the Config Template lives.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"id": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up the Config Template by `id`. Required when `name` is omitted.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRelative(),
path.MatchRoot("name"),
}...),
},
},
"name": dataSourceSchema.StringAttribute{
MarkdownDescription: "Populate this field to look up an imported Config Template by `name`. Required when `id` is omitted.",
Optional: true,
Computed: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"text": dataSourceSchema.StringAttribute{
MarkdownDescription: "Configuration Jinja2 template text",
Computed: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"tags": dataSourceSchema.SetAttribute{
MarkdownDescription: "Set of Tag labels",
ElementType: types.StringType,
Optional: true,
Validators: []validator.Set{setvalidator.SizeAtLeast(1)},
},
}
}
func (o FreeformConfigTemplate) ResourceAttributes() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"id": resourceSchema.StringAttribute{
MarkdownDescription: "ID of the Config Template.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"name": resourceSchema.StringAttribute{
MarkdownDescription: "Config Template name as shown in the Web UI. Must end with `.jinja`.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(7),
stringvalidator.RegexMatches(regexp.MustCompile(".jinja$"), "must end with '.jinja'"),
},
},
"text": resourceSchema.StringAttribute{
MarkdownDescription: "Configuration Jinja2 template text",
Required: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
"tags": resourceSchema.SetAttribute{
MarkdownDescription: "Set of Tag labels",
ElementType: types.StringType,
Optional: true,
Validators: []validator.Set{setvalidator.SizeAtLeast(1)},
},
}
}
func (o *FreeformConfigTemplate) Request(ctx context.Context, diags *diag.Diagnostics) *apstra.ConfigTemplateData {
var tags []string
diags.Append(o.Tags.ElementsAs(ctx, &tags, false)...)
if diags.HasError() {
return nil
}
return &apstra.ConfigTemplateData{
Label: o.Name.ValueString(),
Text: o.Text.ValueString(),
Tags: tags,
}
}
func (o *FreeformConfigTemplate) LoadApiData(ctx context.Context, in *apstra.ConfigTemplateData, diags *diag.Diagnostics) {
o.Name = types.StringValue(in.Label)
o.Text = types.StringValue(in.Text)
o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags) // safe to ignore diagnostic here
}