...

Source file src/github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights/alertrules.go

Documentation: github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights

     1  package insights
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  //
     6  // Code generated by Microsoft (R) AutoRest Code Generator.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  import (
    10  	"context"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/autorest/validation"
    14  	"github.com/Azure/go-autorest/tracing"
    15  	"net/http"
    16  )
    17  
    18  // AlertRulesClient is the monitor Management Client
    19  type AlertRulesClient struct {
    20  	BaseClient
    21  }
    22  
    23  // NewAlertRulesClient creates an instance of the AlertRulesClient client.
    24  func NewAlertRulesClient(subscriptionID string) AlertRulesClient {
    25  	return NewAlertRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
    26  }
    27  
    28  // NewAlertRulesClientWithBaseURI creates an instance of the AlertRulesClient client using a custom endpoint.  Use this
    29  // when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    30  func NewAlertRulesClientWithBaseURI(baseURI string, subscriptionID string) AlertRulesClient {
    31  	return AlertRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
    32  }
    33  
    34  // CreateOrUpdate creates or updates a classic metric alert rule.
    35  // Parameters:
    36  // resourceGroupName - the name of the resource group.
    37  // ruleName - the name of the rule.
    38  // parameters - the parameters of the rule to create or update.
    39  func (client AlertRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, ruleName string, parameters AlertRuleResource) (result AlertRuleResource, err error) {
    40  	if tracing.IsEnabled() {
    41  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.CreateOrUpdate")
    42  		defer func() {
    43  			sc := -1
    44  			if result.Response.Response != nil {
    45  				sc = result.Response.Response.StatusCode
    46  			}
    47  			tracing.EndSpan(ctx, sc, err)
    48  		}()
    49  	}
    50  	if err := validation.Validate([]validation.Validation{
    51  		{TargetValue: parameters,
    52  			Constraints: []validation.Constraint{{Target: "parameters.AlertRule", Name: validation.Null, Rule: true,
    53  				Chain: []validation.Constraint{{Target: "parameters.AlertRule.Name", Name: validation.Null, Rule: true, Chain: nil},
    54  					{Target: "parameters.AlertRule.IsEnabled", Name: validation.Null, Rule: true, Chain: nil},
    55  				}}}}}); err != nil {
    56  		return result, validation.NewError("insights.AlertRulesClient", "CreateOrUpdate", err.Error())
    57  	}
    58  
    59  	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, ruleName, parameters)
    60  	if err != nil {
    61  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
    62  		return
    63  	}
    64  
    65  	resp, err := client.CreateOrUpdateSender(req)
    66  	if err != nil {
    67  		result.Response = autorest.Response{Response: resp}
    68  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", resp, "Failure sending request")
    69  		return
    70  	}
    71  
    72  	result, err = client.CreateOrUpdateResponder(resp)
    73  	if err != nil {
    74  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", resp, "Failure responding to request")
    75  		return
    76  	}
    77  
    78  	return
    79  }
    80  
    81  // CreateOrUpdatePreparer prepares the CreateOrUpdate request.
    82  func (client AlertRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, ruleName string, parameters AlertRuleResource) (*http.Request, error) {
    83  	pathParameters := map[string]interface{}{
    84  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
    85  		"ruleName":          autorest.Encode("path", ruleName),
    86  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
    87  	}
    88  
    89  	const APIVersion = "2016-03-01"
    90  	queryParameters := map[string]interface{}{
    91  		"api-version": APIVersion,
    92  	}
    93  
    94  	preparer := autorest.CreatePreparer(
    95  		autorest.AsContentType("application/json; charset=utf-8"),
    96  		autorest.AsPut(),
    97  		autorest.WithBaseURL(client.BaseURI),
    98  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
    99  		autorest.WithJSON(parameters),
   100  		autorest.WithQueryParameters(queryParameters))
   101  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   102  }
   103  
   104  // CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
   105  // http.Response Body if it receives an error.
   106  func (client AlertRulesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
   107  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   108  }
   109  
   110  // CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
   111  // closes the http.Response Body.
   112  func (client AlertRulesClient) CreateOrUpdateResponder(resp *http.Response) (result AlertRuleResource, err error) {
   113  	err = autorest.Respond(
   114  		resp,
   115  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   116  		autorest.ByUnmarshallingJSON(&result),
   117  		autorest.ByClosing())
   118  	result.Response = autorest.Response{Response: resp}
   119  	return
   120  }
   121  
   122  // Delete deletes a classic metric alert rule
   123  // Parameters:
   124  // resourceGroupName - the name of the resource group.
   125  // ruleName - the name of the rule.
   126  func (client AlertRulesClient) Delete(ctx context.Context, resourceGroupName string, ruleName string) (result autorest.Response, err error) {
   127  	if tracing.IsEnabled() {
   128  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Delete")
   129  		defer func() {
   130  			sc := -1
   131  			if result.Response != nil {
   132  				sc = result.Response.StatusCode
   133  			}
   134  			tracing.EndSpan(ctx, sc, err)
   135  		}()
   136  	}
   137  	req, err := client.DeletePreparer(ctx, resourceGroupName, ruleName)
   138  	if err != nil {
   139  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", nil, "Failure preparing request")
   140  		return
   141  	}
   142  
   143  	resp, err := client.DeleteSender(req)
   144  	if err != nil {
   145  		result.Response = resp
   146  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", resp, "Failure sending request")
   147  		return
   148  	}
   149  
   150  	result, err = client.DeleteResponder(resp)
   151  	if err != nil {
   152  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", resp, "Failure responding to request")
   153  		return
   154  	}
   155  
   156  	return
   157  }
   158  
   159  // DeletePreparer prepares the Delete request.
   160  func (client AlertRulesClient) DeletePreparer(ctx context.Context, resourceGroupName string, ruleName string) (*http.Request, error) {
   161  	pathParameters := map[string]interface{}{
   162  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   163  		"ruleName":          autorest.Encode("path", ruleName),
   164  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   165  	}
   166  
   167  	const APIVersion = "2016-03-01"
   168  	queryParameters := map[string]interface{}{
   169  		"api-version": APIVersion,
   170  	}
   171  
   172  	preparer := autorest.CreatePreparer(
   173  		autorest.AsDelete(),
   174  		autorest.WithBaseURL(client.BaseURI),
   175  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
   176  		autorest.WithQueryParameters(queryParameters))
   177  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   178  }
   179  
   180  // DeleteSender sends the Delete request. The method will close the
   181  // http.Response Body if it receives an error.
   182  func (client AlertRulesClient) DeleteSender(req *http.Request) (*http.Response, error) {
   183  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   184  }
   185  
   186  // DeleteResponder handles the response to the Delete request. The method always
   187  // closes the http.Response Body.
   188  func (client AlertRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
   189  	err = autorest.Respond(
   190  		resp,
   191  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
   192  		autorest.ByClosing())
   193  	result.Response = resp
   194  	return
   195  }
   196  
   197  // Get gets a classic metric alert rule
   198  // Parameters:
   199  // resourceGroupName - the name of the resource group.
   200  // ruleName - the name of the rule.
   201  func (client AlertRulesClient) Get(ctx context.Context, resourceGroupName string, ruleName string) (result AlertRuleResource, err error) {
   202  	if tracing.IsEnabled() {
   203  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Get")
   204  		defer func() {
   205  			sc := -1
   206  			if result.Response.Response != nil {
   207  				sc = result.Response.Response.StatusCode
   208  			}
   209  			tracing.EndSpan(ctx, sc, err)
   210  		}()
   211  	}
   212  	req, err := client.GetPreparer(ctx, resourceGroupName, ruleName)
   213  	if err != nil {
   214  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", nil, "Failure preparing request")
   215  		return
   216  	}
   217  
   218  	resp, err := client.GetSender(req)
   219  	if err != nil {
   220  		result.Response = autorest.Response{Response: resp}
   221  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", resp, "Failure sending request")
   222  		return
   223  	}
   224  
   225  	result, err = client.GetResponder(resp)
   226  	if err != nil {
   227  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", resp, "Failure responding to request")
   228  		return
   229  	}
   230  
   231  	return
   232  }
   233  
   234  // GetPreparer prepares the Get request.
   235  func (client AlertRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, ruleName string) (*http.Request, error) {
   236  	pathParameters := map[string]interface{}{
   237  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   238  		"ruleName":          autorest.Encode("path", ruleName),
   239  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   240  	}
   241  
   242  	const APIVersion = "2016-03-01"
   243  	queryParameters := map[string]interface{}{
   244  		"api-version": APIVersion,
   245  	}
   246  
   247  	preparer := autorest.CreatePreparer(
   248  		autorest.AsGet(),
   249  		autorest.WithBaseURL(client.BaseURI),
   250  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
   251  		autorest.WithQueryParameters(queryParameters))
   252  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   253  }
   254  
   255  // GetSender sends the Get request. The method will close the
   256  // http.Response Body if it receives an error.
   257  func (client AlertRulesClient) GetSender(req *http.Request) (*http.Response, error) {
   258  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   259  }
   260  
   261  // GetResponder handles the response to the Get request. The method always
   262  // closes the http.Response Body.
   263  func (client AlertRulesClient) GetResponder(resp *http.Response) (result AlertRuleResource, err error) {
   264  	err = autorest.Respond(
   265  		resp,
   266  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   267  		autorest.ByUnmarshallingJSON(&result),
   268  		autorest.ByClosing())
   269  	result.Response = autorest.Response{Response: resp}
   270  	return
   271  }
   272  
   273  // ListByResourceGroup list the classic metric alert rules within a resource group.
   274  // Parameters:
   275  // resourceGroupName - the name of the resource group.
   276  func (client AlertRulesClient) ListByResourceGroup(ctx context.Context, resourceGroupName string) (result AlertRuleResourceCollection, err error) {
   277  	if tracing.IsEnabled() {
   278  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.ListByResourceGroup")
   279  		defer func() {
   280  			sc := -1
   281  			if result.Response.Response != nil {
   282  				sc = result.Response.Response.StatusCode
   283  			}
   284  			tracing.EndSpan(ctx, sc, err)
   285  		}()
   286  	}
   287  	req, err := client.ListByResourceGroupPreparer(ctx, resourceGroupName)
   288  	if err != nil {
   289  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", nil, "Failure preparing request")
   290  		return
   291  	}
   292  
   293  	resp, err := client.ListByResourceGroupSender(req)
   294  	if err != nil {
   295  		result.Response = autorest.Response{Response: resp}
   296  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", resp, "Failure sending request")
   297  		return
   298  	}
   299  
   300  	result, err = client.ListByResourceGroupResponder(resp)
   301  	if err != nil {
   302  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", resp, "Failure responding to request")
   303  		return
   304  	}
   305  
   306  	return
   307  }
   308  
   309  // ListByResourceGroupPreparer prepares the ListByResourceGroup request.
   310  func (client AlertRulesClient) ListByResourceGroupPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
   311  	pathParameters := map[string]interface{}{
   312  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   313  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   314  	}
   315  
   316  	const APIVersion = "2016-03-01"
   317  	queryParameters := map[string]interface{}{
   318  		"api-version": APIVersion,
   319  	}
   320  
   321  	preparer := autorest.CreatePreparer(
   322  		autorest.AsGet(),
   323  		autorest.WithBaseURL(client.BaseURI),
   324  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules", pathParameters),
   325  		autorest.WithQueryParameters(queryParameters))
   326  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   327  }
   328  
   329  // ListByResourceGroupSender sends the ListByResourceGroup request. The method will close the
   330  // http.Response Body if it receives an error.
   331  func (client AlertRulesClient) ListByResourceGroupSender(req *http.Request) (*http.Response, error) {
   332  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   333  }
   334  
   335  // ListByResourceGroupResponder handles the response to the ListByResourceGroup request. The method always
   336  // closes the http.Response Body.
   337  func (client AlertRulesClient) ListByResourceGroupResponder(resp *http.Response) (result AlertRuleResourceCollection, err error) {
   338  	err = autorest.Respond(
   339  		resp,
   340  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   341  		autorest.ByUnmarshallingJSON(&result),
   342  		autorest.ByClosing())
   343  	result.Response = autorest.Response{Response: resp}
   344  	return
   345  }
   346  
   347  // ListBySubscription list the classic metric alert rules within a subscription.
   348  func (client AlertRulesClient) ListBySubscription(ctx context.Context) (result AlertRuleResourceCollection, err error) {
   349  	if tracing.IsEnabled() {
   350  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.ListBySubscription")
   351  		defer func() {
   352  			sc := -1
   353  			if result.Response.Response != nil {
   354  				sc = result.Response.Response.StatusCode
   355  			}
   356  			tracing.EndSpan(ctx, sc, err)
   357  		}()
   358  	}
   359  	req, err := client.ListBySubscriptionPreparer(ctx)
   360  	if err != nil {
   361  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", nil, "Failure preparing request")
   362  		return
   363  	}
   364  
   365  	resp, err := client.ListBySubscriptionSender(req)
   366  	if err != nil {
   367  		result.Response = autorest.Response{Response: resp}
   368  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", resp, "Failure sending request")
   369  		return
   370  	}
   371  
   372  	result, err = client.ListBySubscriptionResponder(resp)
   373  	if err != nil {
   374  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", resp, "Failure responding to request")
   375  		return
   376  	}
   377  
   378  	return
   379  }
   380  
   381  // ListBySubscriptionPreparer prepares the ListBySubscription request.
   382  func (client AlertRulesClient) ListBySubscriptionPreparer(ctx context.Context) (*http.Request, error) {
   383  	pathParameters := map[string]interface{}{
   384  		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
   385  	}
   386  
   387  	const APIVersion = "2016-03-01"
   388  	queryParameters := map[string]interface{}{
   389  		"api-version": APIVersion,
   390  	}
   391  
   392  	preparer := autorest.CreatePreparer(
   393  		autorest.AsGet(),
   394  		autorest.WithBaseURL(client.BaseURI),
   395  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/microsoft.insights/alertrules", pathParameters),
   396  		autorest.WithQueryParameters(queryParameters))
   397  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   398  }
   399  
   400  // ListBySubscriptionSender sends the ListBySubscription request. The method will close the
   401  // http.Response Body if it receives an error.
   402  func (client AlertRulesClient) ListBySubscriptionSender(req *http.Request) (*http.Response, error) {
   403  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   404  }
   405  
   406  // ListBySubscriptionResponder handles the response to the ListBySubscription request. The method always
   407  // closes the http.Response Body.
   408  func (client AlertRulesClient) ListBySubscriptionResponder(resp *http.Response) (result AlertRuleResourceCollection, err error) {
   409  	err = autorest.Respond(
   410  		resp,
   411  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   412  		autorest.ByUnmarshallingJSON(&result),
   413  		autorest.ByClosing())
   414  	result.Response = autorest.Response{Response: resp}
   415  	return
   416  }
   417  
   418  // Update updates an existing classic metric AlertRuleResource. To update other fields use the CreateOrUpdate method.
   419  // Parameters:
   420  // resourceGroupName - the name of the resource group.
   421  // ruleName - the name of the rule.
   422  // alertRulesResource - parameters supplied to the operation.
   423  func (client AlertRulesClient) Update(ctx context.Context, resourceGroupName string, ruleName string, alertRulesResource AlertRuleResourcePatch) (result AlertRuleResource, err error) {
   424  	if tracing.IsEnabled() {
   425  		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Update")
   426  		defer func() {
   427  			sc := -1
   428  			if result.Response.Response != nil {
   429  				sc = result.Response.Response.StatusCode
   430  			}
   431  			tracing.EndSpan(ctx, sc, err)
   432  		}()
   433  	}
   434  	req, err := client.UpdatePreparer(ctx, resourceGroupName, ruleName, alertRulesResource)
   435  	if err != nil {
   436  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", nil, "Failure preparing request")
   437  		return
   438  	}
   439  
   440  	resp, err := client.UpdateSender(req)
   441  	if err != nil {
   442  		result.Response = autorest.Response{Response: resp}
   443  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", resp, "Failure sending request")
   444  		return
   445  	}
   446  
   447  	result, err = client.UpdateResponder(resp)
   448  	if err != nil {
   449  		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", resp, "Failure responding to request")
   450  		return
   451  	}
   452  
   453  	return
   454  }
   455  
   456  // UpdatePreparer prepares the Update request.
   457  func (client AlertRulesClient) UpdatePreparer(ctx context.Context, resourceGroupName string, ruleName string, alertRulesResource AlertRuleResourcePatch) (*http.Request, error) {
   458  	pathParameters := map[string]interface{}{
   459  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   460  		"ruleName":          autorest.Encode("path", ruleName),
   461  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   462  	}
   463  
   464  	const APIVersion = "2016-03-01"
   465  	queryParameters := map[string]interface{}{
   466  		"api-version": APIVersion,
   467  	}
   468  
   469  	preparer := autorest.CreatePreparer(
   470  		autorest.AsContentType("application/json; charset=utf-8"),
   471  		autorest.AsPatch(),
   472  		autorest.WithBaseURL(client.BaseURI),
   473  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
   474  		autorest.WithJSON(alertRulesResource),
   475  		autorest.WithQueryParameters(queryParameters))
   476  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   477  }
   478  
   479  // UpdateSender sends the Update request. The method will close the
   480  // http.Response Body if it receives an error.
   481  func (client AlertRulesClient) UpdateSender(req *http.Request) (*http.Response, error) {
   482  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   483  }
   484  
   485  // UpdateResponder handles the response to the Update request. The method always
   486  // closes the http.Response Body.
   487  func (client AlertRulesClient) UpdateResponder(resp *http.Response) (result AlertRuleResource, err error) {
   488  	err = autorest.Respond(
   489  		resp,
   490  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   491  		autorest.ByUnmarshallingJSON(&result),
   492  		autorest.ByClosing())
   493  	result.Response = autorest.Response{Response: resp}
   494  	return
   495  }
   496  

View as plain text