-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathschema_extensions.go
155 lines (131 loc) · 4.38 KB
/
schema_extensions.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
146
147
148
149
150
151
152
153
154
155
package msgraph
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
// SchemaExtensionsClient performs operations on Schema Extensions.
type SchemaExtensionsClient struct {
BaseClient Client
}
// NewSchemaExtensionsClient returns a new SchemaExtensionsClient.
func NewSchemaExtensionsClient() *SchemaExtensionsClient {
return &SchemaExtensionsClient{
BaseClient: NewClient(VersionBeta),
}
}
// List returns a list of Schema Extensions, optionally filtered using OData.
func (c *SchemaExtensionsClient) List(ctx context.Context, query odata.Query) (*[]SchemaExtension, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
DisablePaging: query.Top > 0,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/schemaExtensions",
},
})
if err != nil {
return nil, status, fmt.Errorf("SchemaExtensionsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var data struct {
SchemaExtensions []SchemaExtension `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data.SchemaExtensions, status, nil
}
// Get retrieves a Schema Extension.
func (c *SchemaExtensionsClient) Get(ctx context.Context, id string, query odata.Query) (*SchemaExtension, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/schemaExtensions/%s", id),
},
})
if err != nil {
return nil, status, fmt.Errorf("SchemaExtensionsClient.BaseClient.Get(): %v", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var schemaExtension SchemaExtension
if err := json.Unmarshal(respBody, &schemaExtension); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &schemaExtension, status, nil
}
// Update amends an existing schema Extension.
func (c *SchemaExtensionsClient) Update(ctx context.Context, schemaExtension SchemaExtension) (int, error) {
var status int
body, err := json.Marshal(schemaExtension)
if err != nil {
return status, fmt.Errorf("json.Marshal(): %v", err)
}
_, status, _, err = c.BaseClient.Patch(ctx, PatchHttpRequestInput{
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/schemaExtensions/%s", *schemaExtension.ID),
},
})
if err != nil {
return status, fmt.Errorf("SchemaExtensionsClient.BaseClient.Patch(): %v", err)
}
return status, nil
}
// Create creates a new Schema Extension
func (c *SchemaExtensionsClient) Create(ctx context.Context, schemaExtension SchemaExtension) (*SchemaExtension, int, error) {
var status int
body, err := json.Marshal(schemaExtension)
if err != nil {
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: "/schemaExtensions",
},
})
if err != nil {
return nil, status, fmt.Errorf("SchemaExtensionsClient.BaseClient.Post(): %v", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}
var newSchemaExtension SchemaExtension
if err := json.Unmarshal(respBody, &newSchemaExtension); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &newSchemaExtension, status, nil
}
// Delete removes a schema extension.
func (c *SchemaExtensionsClient) Delete(ctx context.Context, id string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/schemaExtensions/%s", id),
},
})
if err != nil {
return status, fmt.Errorf("SchemaExtensionsClient.BaseClient.Delete(): %v", err)
}
return status, nil
}