1package managementgroups
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/tracing"
25	"net/http"
26)
27
28// SubscriptionsClient is the the Azure Management Groups API enables consolidation of multiple
29// subscriptions/resources into an organizational hierarchy and centrally
30// manage access control, policies, alerting and reporting for those resources.
31type SubscriptionsClient struct {
32	BaseClient
33}
34
35// NewSubscriptionsClient creates an instance of the SubscriptionsClient client.
36func NewSubscriptionsClient() SubscriptionsClient {
37	return NewSubscriptionsClientWithBaseURI(DefaultBaseURI)
38}
39
40// NewSubscriptionsClientWithBaseURI creates an instance of the SubscriptionsClient client using a custom endpoint.
41// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
42func NewSubscriptionsClientWithBaseURI(baseURI string) SubscriptionsClient {
43	return SubscriptionsClient{NewWithBaseURI(baseURI)}
44}
45
46// Create associates existing subscription with the management group.
47// Parameters:
48// groupID - management Group ID.
49// subscriptionID - subscription ID.
50// cacheControl - indicates that the request shouldn't utilize any caches.
51func (client SubscriptionsClient) Create(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (result autorest.Response, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionsClient.Create")
54		defer func() {
55			sc := -1
56			if result.Response != nil {
57				sc = result.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	req, err := client.CreatePreparer(ctx, groupID, subscriptionID, cacheControl)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Create", nil, "Failure preparing request")
65		return
66	}
67
68	resp, err := client.CreateSender(req)
69	if err != nil {
70		result.Response = resp
71		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Create", resp, "Failure sending request")
72		return
73	}
74
75	result, err = client.CreateResponder(resp)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Create", resp, "Failure responding to request")
78	}
79
80	return
81}
82
83// CreatePreparer prepares the Create request.
84func (client SubscriptionsClient) CreatePreparer(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (*http.Request, error) {
85	pathParameters := map[string]interface{}{
86		"groupId":        autorest.Encode("path", groupID),
87		"subscriptionId": autorest.Encode("path", subscriptionID),
88	}
89
90	const APIVersion = "2017-11-01-preview"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsPut(),
97		autorest.WithBaseURL(client.BaseURI),
98		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}", pathParameters),
99		autorest.WithQueryParameters(queryParameters))
100	if len(cacheControl) > 0 {
101		preparer = autorest.DecoratePreparer(preparer,
102			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
103	} else {
104		preparer = autorest.DecoratePreparer(preparer,
105			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
106	}
107	return preparer.Prepare((&http.Request{}).WithContext(ctx))
108}
109
110// CreateSender sends the Create request. The method will close the
111// http.Response Body if it receives an error.
112func (client SubscriptionsClient) CreateSender(req *http.Request) (*http.Response, error) {
113	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
114}
115
116// CreateResponder handles the response to the Create request. The method always
117// closes the http.Response Body.
118func (client SubscriptionsClient) CreateResponder(resp *http.Response) (result autorest.Response, err error) {
119	err = autorest.Respond(
120		resp,
121		client.ByInspecting(),
122		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
123		autorest.ByClosing())
124	result.Response = resp
125	return
126}
127
128// Delete de-associates subscription from the management group.
129// Parameters:
130// groupID - management Group ID.
131// subscriptionID - subscription ID.
132// cacheControl - indicates that the request shouldn't utilize any caches.
133func (client SubscriptionsClient) Delete(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (result autorest.Response, err error) {
134	if tracing.IsEnabled() {
135		ctx = tracing.StartSpan(ctx, fqdn+"/SubscriptionsClient.Delete")
136		defer func() {
137			sc := -1
138			if result.Response != nil {
139				sc = result.Response.StatusCode
140			}
141			tracing.EndSpan(ctx, sc, err)
142		}()
143	}
144	req, err := client.DeletePreparer(ctx, groupID, subscriptionID, cacheControl)
145	if err != nil {
146		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Delete", nil, "Failure preparing request")
147		return
148	}
149
150	resp, err := client.DeleteSender(req)
151	if err != nil {
152		result.Response = resp
153		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Delete", resp, "Failure sending request")
154		return
155	}
156
157	result, err = client.DeleteResponder(resp)
158	if err != nil {
159		err = autorest.NewErrorWithError(err, "managementgroups.SubscriptionsClient", "Delete", resp, "Failure responding to request")
160	}
161
162	return
163}
164
165// DeletePreparer prepares the Delete request.
166func (client SubscriptionsClient) DeletePreparer(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (*http.Request, error) {
167	pathParameters := map[string]interface{}{
168		"groupId":        autorest.Encode("path", groupID),
169		"subscriptionId": autorest.Encode("path", subscriptionID),
170	}
171
172	const APIVersion = "2017-11-01-preview"
173	queryParameters := map[string]interface{}{
174		"api-version": APIVersion,
175	}
176
177	preparer := autorest.CreatePreparer(
178		autorest.AsDelete(),
179		autorest.WithBaseURL(client.BaseURI),
180		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}", pathParameters),
181		autorest.WithQueryParameters(queryParameters))
182	if len(cacheControl) > 0 {
183		preparer = autorest.DecoratePreparer(preparer,
184			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
185	} else {
186		preparer = autorest.DecoratePreparer(preparer,
187			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
188	}
189	return preparer.Prepare((&http.Request{}).WithContext(ctx))
190}
191
192// DeleteSender sends the Delete request. The method will close the
193// http.Response Body if it receives an error.
194func (client SubscriptionsClient) DeleteSender(req *http.Request) (*http.Response, error) {
195	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
196}
197
198// DeleteResponder handles the response to the Delete request. The method always
199// closes the http.Response Body.
200func (client SubscriptionsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
201	err = autorest.Respond(
202		resp,
203		client.ByInspecting(),
204		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
205		autorest.ByClosing())
206	result.Response = resp
207	return
208}
209