/
access-ports.go
74 lines (65 loc) · 1.86 KB
/
access-ports.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
package provider
import (
"context"
protos "github.com/H-BF/protos/pkg/api/sgroups"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type (
AccessPorts struct {
Source types.String `tfsdk:"s"`
Destination types.String `tfsdk:"d"`
}
)
func (p AccessPorts) Attributes() map[string]schema.Attribute {
return map[string]schema.Attribute{
"s": schema.StringAttribute{
Description: "source port/ports range",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"d": schema.StringAttribute{
Description: "destination port/ports range",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
}
}
func (p AccessPorts) AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"s": types.StringType,
"d": types.StringType,
}
}
func (p AccessPorts) toProto() *protos.AccPorts {
return &protos.AccPorts{
S: p.Source.ValueString(),
D: p.Destination.ValueString(),
}
}
func accPortsRangeToProto(data []AccessPorts) []*protos.AccPorts {
var ret []*protos.AccPorts
for _, port := range data {
ret = append(ret, port.toProto())
}
return ret
}
func accPortsRangeFromTF(ctx context.Context, tfValue types.List) (ret []AccessPorts, diags diag.Diagnostics) {
diags.Append(tfValue.ElementsAs(ctx, &ret, false)...)
//if diags.HasError() {
// return ret, diags
//}
// this conversion necessary to validate string with ports
/*//TODO: whether it`s really necessary?
if _, err := toModelPorts(ret); err != nil {
diags.AddError("ports conv", err.Error())
return ret, diags
}
*/
return ret, diags
}