Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates Group Quota Samples #381

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions sdk/resourcemanager/quota/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
page_type: sample
languages:
- go
products:
- azure
description: "These code samples will show you how to manage Group Quota Resources using Azure SDK for Golang."
---

# Getting started - Managing Group Quota Resources using Azure Golang SDK

These code samples will show you how to manage Group Quota Resources using Azure SDK for Golang.

## Features

This project framework provides examples for the following services:

### Database for MySQL
* Using the Azure SDK for Golang - Quota Management Library [quota](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota@v1.1.0-beta.1 )

If the link is going to non existant page try this [link](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota?tab=versions) then navigate to the v1.1.0-beta.1 version

### Prerequisites
* an [Azure subscription](https://azure.microsoft.com)
* Go 1.18 or above

### Quickstart

1. Clone the repository.

```
git clone https://github.com/Azure-Samples/azure-sdk-for-go-samples.git
```

2. Set the environment variable.

```
# bash
export AZURE_SUBSCRIPTION_ID=<your Azure subscription id>
# If no value is set, the created resource will be deleted by default.
# anything other than empty to keep the resources
export KEEP_RESOURCE=1
export AZURE_TENANT_ID=<your Azure Tenant id>
export AZURE_OBJECT_ID=<your Azure Client/Object id>
```

3. Run quota sample.

```
cd azure-sdk-for-go-samples/sdk/resourcemanager/quota
go mod tidy
go run main.go
```

## Resources

- https://github.com/Azure/azure-sdk-for-go
- https://docs.microsoft.com/en-us/azure/developer/go/
- https://docs.microsoft.com/en-us/rest/api/
- https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk

## Need help?

Post issue on Github (https://github.com/Azure/azure-sdk-for-go/issues)
32 changes: 32 additions & 0 deletions sdk/resourcemanager/quota/addSubscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"log"
)

func addSubscription(groupName string) {
ctx := context.Background()
clientFactory := createClientFactory()

poller, err := clientFactory.NewGroupQuotaSubscriptionsClient().BeginCreateOrUpdate(ctx, managementGroupId, groupName, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
res, err := poller.PollUntilDone(ctx, nil)
if err != nil {
log.Fatalf("failed to pull the result: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res.GroupQuotaSubscriptionID = armquota.GroupQuotaSubscriptionID{
// Name: to.Ptr("00000000-0000-0000-0000-000000000000"),
// Type: to.Ptr("Microsoft.Quota/groupQuotas/subscriptions"),
// ID: to.Ptr("/providers/Microsoft.Management/managementGroups/E7EC67B3-7657-4966-BFFC-41EFD36BAA09/providers/Microsoft.Quota/groupQuotas/groupquota1/subscriptions/00000000-0000-0000-0000-000000000000"),
// Properties: &armquota.GroupQuotaSubscriptionIDProperties{
// ProvisioningState: to.Ptr(armquota.RequestStateSucceeded),
// SubscriptionID: to.Ptr("00000000-0000-0000-0000-000000000000"),
// },
// }
}
46 changes: 46 additions & 0 deletions sdk/resourcemanager/quota/createGroupQuota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota"
)

func createGroupQuota(groupName string, managementGroupId string) {
clientFactory := createClientFactory()
ctx := context.Background()

groupQuotaBody := &armquota.GroupQuotasClientBeginCreateOrUpdateOptions{
GroupQuotaPutRequestBody: &armquota.GroupQuotasEntity{
Properties: &armquota.GroupQuotasEntityBase{
AdditionalAttributes: &armquota.AdditionalAttributes{
GroupID: &armquota.GroupingID{
GroupingIDType: to.Ptr(armquota.GroupingIDTypeBillingID),
Value: to.Ptr("E7EC67B3-7657-4966-BFFC-41EFD36BAA09"),
},
},
DisplayName: to.Ptr(groupName),
},
},
}

poller, err := clientFactory.NewGroupQuotasClient().BeginCreateOrUpdate(ctx, managementGroupId, groupName, groupQuotaBody)

if err != nil {
log.Fatalf("failed to create GroupQuota: %v", err)
}

fmt.Println("waiting for GroupQuota creation to complete")

res, err := poller.PollUntilDone(ctx, nil)

if err != nil {
log.Fatalf("failed to poll the result of the request: %v", err)
}

_ = res
//return res
}
114 changes: 114 additions & 0 deletions sdk/resourcemanager/quota/createGroupQuotaLimitRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"context"
"fmt"
"log"
"net/url"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota"
)

func createGroupQuotaLimitRequest(groupName string, provider string, resourceName string, region string, limitVal int64) {
ctx := context.Background()
clientFactory := createClientFactory()

groupQuotaLimitRequestBody := &armquota.GroupQuotaLimitsRequestClientBeginCreateOrUpdateOptions{
GroupQuotaRequest: &armquota.SubmittedResourceRequestStatus{
Properties: &armquota.SubmittedResourceRequestStatusProperties{
RequestedResource: &armquota.GroupQuotaRequestBase{
Properties: &armquota.GroupQuotaRequestBaseProperties{
Limit: &limitVal,
Region: to.Ptr(region),
},
},
},
},
}
groupQuotaLimitsRequestClient := clientFactory.NewGroupQuotaLimitsRequestClient()
poller, err := groupQuotaLimitsRequestClient.BeginCreateOrUpdate(ctx, managementGroupId, groupName, provider, resourceName, groupQuotaLimitRequestBody)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}

// Get the HTTP response from the quota limit request
res, err := poller.Poll(ctx)
if err != nil {
panic(err)
}
opStatus := res.Header["Location"][0]
opStatusURI, err := url.Parse(opStatus)
if err != nil {
panic(err)
}

// Get the request ID and the Request URL in case the request doesn't reach a terminal state in 2 minutes
opStatusURISegments := strings.Split(opStatusURI.Path, "/")
id := opStatusURISegments[len(opStatusURISegments)-1]

// Poll for 2 minutes until the group quota limit request has completed
start := time.Now()
duration := 2 * time.Minute
provisioningState := armquota.RequestStateInProgress
for time.Since(start) < duration {
group_limit_request_res, err := groupQuotaLimitsRequestClient.Get(ctx, managementGroupId, groupName, id, nil)
if err != nil {
panic(err)
}

provisioningState = *group_limit_request_res.Properties.ProvisioningState
if provisioningState == armquota.RequestStateSucceeded {
groupQuotaLimitClient := clientFactory.NewGroupQuotaLimitsClient()
filterString := fmt.Sprintf("location eq %s", region)
limit_res, err := groupQuotaLimitClient.Get(ctx, managementGroupId, groupName, provider, resourceName, filterString, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = limit_res
break
} else if provisioningState == armquota.RequestStateFailed {
fmt.Println("Group Quota Limit Request Failed")
log.Fatalf("Request failed")
}
fmt.Println("Polling...")
time.Sleep(30 * time.Second)
}

if provisioningState == armquota.RequestStateInProgress || provisioningState == "Escalated" {
fmt.Println("Did not reach terminal state within 2 minutes. Please perform a get on this URL: %s", opStatus)
}

if provisioningState == "Escalated" {
fmt.Println("Request was escalated please contact your capacity manager. Please perform a get on this URL: %s", opStatus)
}

// You could use response here. We use blank identifier for just demo purposes.
//_ = limit_res
// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res.GroupQuotaLimit = armquota.GroupQuotaLimit{
// Name: to.Ptr("cores"),
// Type: to.Ptr("Microsoft.Quota/groupQuotas/groupQuotaLimits"),
// ID: to.Ptr("/providers/Microsoft.Management/managementGroups/E7EC67B3-7657-4966-BFFC-41EFD36BAA09/providers/Microsoft.Quota/groupQuotas/groupquota1/providers/Microsoft.Compute/locations/westus/groupQuotaLimits/cores"),
// Properties: &armquota.GroupQuotaDetails{
// Name: &armquota.GroupQuotaDetailsName{
// LocalizedValue: to.Ptr("Total vCPUs Regional Cores"),
// Value: to.Ptr("cores"),
// },
// AllocatedToSubscriptions: &armquota.AllocatedQuotaToSubscriptionList{
// Value: []*armquota.AllocatedToSubscription{
// {
// QuotaAllocated: to.Ptr[int64](20),
// SubscriptionID: to.Ptr("00000000-0000-0000-0000-000000000000"),
// }},
// },
// AvailableLimit: to.Ptr[int64](80),
// Limit: to.Ptr[int64](100),
// Region: to.Ptr("westus"),
// Unit: to.Ptr("count"),
// },
// }
}
99 changes: 99 additions & 0 deletions sdk/resourcemanager/quota/createSubscriptionAllocationRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"fmt"
"log"
"net/url"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota"
)

// Generated from example definition: https://github.com/Azure/azure-rest-api-specs/blob/106483d9f698ac3b6c0d481ab0c5fab14152e21f/specification/quota/resource-manager/Microsoft.Quota/preview/2023-06-01-preview/examples/SubscriptionQuotaAllocationRequests/PutSubscriptionQuotaAllocationRequest-Compute.json
func createSubscriptionAllocationRequest(groupName string, provider string, resourceName string, region string, allocVal int64) {
ctx := context.Background()
clientFactory := createClientFactory()
subscriptionQuotaAllocationRequestClient := clientFactory.NewGroupQuotaSubscriptionAllocationRequestClient()
poller, err := subscriptionQuotaAllocationRequestClient.BeginCreateOrUpdate(ctx, managementGroupId, groupName, provider, resourceName, armquota.AllocationRequestStatus{
Properties: &armquota.AllocationRequestStatusProperties{
RequestedResource: &armquota.AllocationRequestBase{
Properties: &armquota.AllocationRequestBaseProperties{
Limit: to.Ptr[int64](allocVal),
Region: to.Ptr(region),
},
},
},
}, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
res, err := poller.Poll(ctx)
if err != nil {
panic(err)
}
opStatus := res.Header["Location"][0]
opStatusURI, err := url.Parse(opStatus)
if err != nil {
panic(err)
}

// Get the request ID and the Request URL in case the request doesn't reach a terminal state in 2 minutes
opStatusURISegments := strings.Split(opStatusURI.Path, "/")
id := opStatusURISegments[len(opStatusURISegments)-1]

// Poll for 2 minutes until the group quota limit request has completed
start := time.Now()
duration := 2 * time.Minute
provisioningState := armquota.RequestStateInProgress
for time.Since(start) < duration {
allocation_res, err := subscriptionQuotaAllocationRequestClient.Get(ctx, managementGroupId, groupName, id, nil)
if err != nil {
panic(err)
}

provisioningState = *allocation_res.Properties.ProvisioningState
if provisioningState == armquota.RequestStateSucceeded {
subscriptionQuotaAllocationClient := clientFactory.NewGroupQuotaSubscriptionAllocationClient()
filterString := fmt.Sprintf("location eq %s", region)
allocation_res, err := subscriptionQuotaAllocationClient.Get(ctx, managementGroupId, groupName, resourceName, filterString, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
// You could use response here. We use blank identifier for just demo purposes.
_ = allocation_res
break
} else if provisioningState == armquota.RequestStateFailed {
fmt.Println("Group Quota Subscription Allocation Failed")
log.Fatalf("Request failed")
}
fmt.Println("Polling...")
time.Sleep(30 * time.Second)
}

if provisioningState == armquota.RequestStateInProgress || provisioningState == armquota.RequestStateAccepted {
fmt.Println("Did not reach terminal state within 2 minutes. Please perform a get on this URL: %s", opStatus)
}

if provisioningState == "Escalated" {
fmt.Println("Request was escalated please contact your capacity manager. Please perform a get on this URL: %s", opStatus)
}

// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
// res.SubscriptionQuotaAllocations = armquota.SubscriptionQuotaAllocations{
// Name: to.Ptr("standardav2family"),
// Type: to.Ptr("Microsoft.Quota/groupQuotas/quotaAllocations"),
// ID: to.Ptr("/providers/Microsoft.Management/managementGroups/E7EC67B3-7657-4966-BFFC-41EFD36BAA09/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Quota/groupQuotas/groupquota1/providers/Microsoft.Compute/locations/westus/quotaAllocations/standardav2family"),
// Properties: &armquota.SubscriptionQuotaDetails{
// Name: &armquota.SubscriptionQuotaDetailsName{
// LocalizedValue: to.Ptr("standard Av2 Family vCPUs"),
// Value: to.Ptr("standardav2family"),
// },
// Limit: to.Ptr[int64](100),
// Region: to.Ptr("westus"),
// ShareableQuota: to.Ptr[int64](25),
// },
// }
}
20 changes: 20 additions & 0 deletions sdk/resourcemanager/quota/deleteGroupQuota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"context"
"log"
)

func deleteGroupQuota(groupName string) {
ctx := context.Background()
clientFactory := createClientFactory()

poller, err := clientFactory.NewGroupQuotasClient().BeginDelete(ctx, managementGroupId, groupName, nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
_, err = poller.PollUntilDone(ctx, nil)
if err != nil {
log.Fatalf("failed to pull the result: %v", err)
}
}
Loading
Loading