-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathapplication_templates.go
120 lines (101 loc) · 3.62 KB
/
application_templates.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
package msgraph
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
// ApplicationTemplatesClient performs operations on ApplicationTemplates.
type ApplicationTemplatesClient struct {
BaseClient Client
}
// NewApplicationTemplatesClient returns a new ApplicationTemplatesClient
func NewApplicationTemplatesClient() *ApplicationTemplatesClient {
return &ApplicationTemplatesClient{
BaseClient: NewClient(Version10),
}
}
// List returns a list of ApplicationTemplates, optionally queried using OData.
func (c *ApplicationTemplatesClient) List(ctx context.Context, query odata.Query) (*[]ApplicationTemplate, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
DisablePaging: query.Top > 0,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/applicationTemplates",
},
})
if err != nil {
return nil, status, fmt.Errorf("ApplicationTemplatesClient.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 {
ApplicationTemplates []ApplicationTemplate `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data.ApplicationTemplates, status, nil
}
// Get retrieves an ApplicationTemplate
func (c *ApplicationTemplatesClient) Get(ctx context.Context, id string, query odata.Query) (*ApplicationTemplate, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/applicationTemplates/%s", id),
},
})
if err != nil {
return nil, status, fmt.Errorf("ApplicationTemplatesClient.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 applicationTemplate ApplicationTemplate
if err := json.Unmarshal(respBody, &applicationTemplate); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &applicationTemplate, status, nil
}
// Instantiate instantiates an ApplicationTemplate, which creates an Application and Service Principal in the tenant.
// The created Application and ServicePrincipal are provided in the response.
func (c *ApplicationTemplatesClient) Instantiate(ctx context.Context, applicationTemplate ApplicationTemplate) (*ApplicationTemplate, int, error) {
var status int
if applicationTemplate.ID == nil {
return nil, status, errors.New("ApplicationTemplatesClient.Instantiate(): cannot instantiate ApplicationTemplate with nil ID")
}
body, err := json.Marshal(applicationTemplate)
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: fmt.Sprintf("/applicationTemplates/%s/instantiate", *applicationTemplate.ID),
},
})
if err != nil {
return nil, status, fmt.Errorf("ApplicationTemplatesClient.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 newApplicationTemplate ApplicationTemplate
if err := json.Unmarshal(respBody, &newApplicationTemplate); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &newApplicationTemplate, status, nil
}