-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasource_documents.go
145 lines (136 loc) · 4.05 KB
/
datasource_documents.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package provider
import (
"context"
"github.com/PacketFabric/terraform-provider-packetfabric/internal/packetfabric"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func datasourceDocuments() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDocumentsRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"documents": {
Type: schema.TypeList,
Description: "Documents list",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the document",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Document name",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Document description",
},
"mime_type": {
Type: schema.TypeString,
Computed: true,
Description: "Mime type of the document",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "Document size",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Document type. One of [\"loa\", \"msa\"]",
},
"time_created": {
Type: schema.TypeString,
Computed: true,
Description: "Document creation time",
},
"time_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Document's last update time",
},
"_links": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Type: schema.TypeString,
Computed: true,
Description: "A link to the port this document refers to",
},
"service": {
Type: schema.TypeString,
Computed: true,
Description: "A link to the service this document refers to",
},
"cloud": {
Type: schema.TypeString,
Computed: true,
Description: "A link to the cloud service this document refers to",
},
"cloud_router": {
Type: schema.TypeString,
Computed: true,
Description: "A link to the cloud service this document refers to",
},
"cloud_router_connection": {
Type: schema.TypeString,
Computed: true,
Description: "A link to the cloud router connection this document refers to",
},
},
},
},
},
},
},
},
}
}
func dataSourceDocumentsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
documents, err := c.GetDocuments()
if err != nil {
return diag.FromErr(err)
}
documentsList := []interface{}{}
for _, document := range documents {
links := schema.NewSet(func(i interface{}) int { return 0 }, []interface{}{})
link := map[string]string{
"port": document.Links.Port,
"service": document.Links.Service,
"cloud": document.Links.Cloud,
"cloud_router": document.Links.CloudRouter,
"cloud_router_connection": document.Links.CloudRouterConnection,
}
links.Add(link)
documentsList = append(documentsList, map[string]interface{}{
"uuid": document.UUID,
"name": document.Name,
"description": document.Description,
"mime_type": document.MimeType,
"size": document.Size,
"type": document.Type,
"time_created": document.TimeCreated,
"time_updated": document.TimeUpdated,
"_links": links,
})
}
_ = d.Set("documents", documentsList)
d.SetId(uuid.NewString())
return diags
}