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(operationResultID string, skip *int32, top *int32, skiptoken string) SubscriptionsClient {
37	return NewSubscriptionsClientWithBaseURI(DefaultBaseURI, operationResultID, skip, top, skiptoken)
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, operationResultID string, skip *int32, top *int32, skiptoken string) SubscriptionsClient {
43	return SubscriptionsClient{NewWithBaseURI(baseURI, operationResultID, skip, top, skiptoken)}
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		return
79	}
80
81	return
82}
83
84// CreatePreparer prepares the Create request.
85func (client SubscriptionsClient) CreatePreparer(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (*http.Request, error) {
86	pathParameters := map[string]interface{}{
87		"groupId":        autorest.Encode("path", groupID),
88		"subscriptionId": autorest.Encode("path", subscriptionID),
89	}
90
91	const APIVersion = "2020-02-01"
92	queryParameters := map[string]interface{}{
93		"api-version": APIVersion,
94	}
95
96	preparer := autorest.CreatePreparer(
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}", pathParameters),
100		autorest.WithQueryParameters(queryParameters))
101	if len(cacheControl) > 0 {
102		preparer = autorest.DecoratePreparer(preparer,
103			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
104	} else {
105		preparer = autorest.DecoratePreparer(preparer,
106			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
107	}
108	return preparer.Prepare((&http.Request{}).WithContext(ctx))
109}
110
111// CreateSender sends the Create request. The method will close the
112// http.Response Body if it receives an error.
113func (client SubscriptionsClient) CreateSender(req *http.Request) (*http.Response, error) {
114	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
115}
116
117// CreateResponder handles the response to the Create request. The method always
118// closes the http.Response Body.
119func (client SubscriptionsClient) CreateResponder(resp *http.Response) (result autorest.Response, err error) {
120	err = autorest.Respond(
121		resp,
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		return
161	}
162
163	return
164}
165
166// DeletePreparer prepares the Delete request.
167func (client SubscriptionsClient) DeletePreparer(ctx context.Context, groupID string, subscriptionID string, cacheControl string) (*http.Request, error) {
168	pathParameters := map[string]interface{}{
169		"groupId":        autorest.Encode("path", groupID),
170		"subscriptionId": autorest.Encode("path", subscriptionID),
171	}
172
173	const APIVersion = "2020-02-01"
174	queryParameters := map[string]interface{}{
175		"api-version": APIVersion,
176	}
177
178	preparer := autorest.CreatePreparer(
179		autorest.AsDelete(),
180		autorest.WithBaseURL(client.BaseURI),
181		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}", pathParameters),
182		autorest.WithQueryParameters(queryParameters))
183	if len(cacheControl) > 0 {
184		preparer = autorest.DecoratePreparer(preparer,
185			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
186	} else {
187		preparer = autorest.DecoratePreparer(preparer,
188			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
189	}
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// DeleteSender sends the Delete request. The method will close the
194// http.Response Body if it receives an error.
195func (client SubscriptionsClient) DeleteSender(req *http.Request) (*http.Response, error) {
196	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
197}
198
199// DeleteResponder handles the response to the Delete request. The method always
200// closes the http.Response Body.
201func (client SubscriptionsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
202	err = autorest.Respond(
203		resp,
204		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
205		autorest.ByClosing())
206	result.Response = resp
207	return
208}
209