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 1 commit
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
Next Next commit
adds quota directory and basic functionality
  • Loading branch information
tejasm-microsoft committed Sep 9, 2024
commit 5e9c50f836e84a53f3e74b3f2fceaddb696aaa73
22 changes: 22 additions & 0 deletions sdk/resourcemanager/quota/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/Azure-Samples/azure-sdk-for-go-samples/sdk/resourcemanager/compute/createVM

go 1.18

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota v1.1.0-beta.1
)

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
33 changes: 33 additions & 0 deletions sdk/resourcemanager/quota/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0 h1:nyQWyZvwGTvunIMxi1Y9uXkcyr+I7TeNrr/foo4Kpk8=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0/go.mod h1:l38EPgmsp71HHLq9j7De57JcKOWPyhrsW1Awm1JS6K0=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 h1:tfLQ34V6F7tVSwoTf/4lH5sE0o6eCJuNDTmH09nDpbc=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0/go.mod h1:9kIvujWAA58nmPmWB1m23fyWic1kYZMxD9CxaWn4Qpg=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFGRSlMKCQelWwxUyYVEUqseBJVemLyqWJjvMyt0do=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota v1.1.0-beta.1 h1:Sa2LkW66Cjr/ZEmgXP6uMfUEj64edkRv7p6ptCmBe/Y=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota v1.1.0-beta.1/go.mod h1:l20qtqMTIhPymxw/sgwafDtbe8TWpfZcTtrM3yHlrrg=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1 h1:7CBQ+Ei8SP2c6ydQTGCCrS35bDxgTMfoP2miAwK++OU=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
259 changes: 259 additions & 0 deletions sdk/resourcemanager/quota/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package main

import (
"context"
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
armPolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota"
)

var subscriptionId string

const (
resourceGroupName = "sample-resource-group"
vmName = "sample-vm"
vnetName = "sample-vnet"
subnetName = "sample-subnet"
nsgName = "sample-nsg"
nicName = "sample-nic"
diskName = "sample-disk"
publicIPName = "sample-public-ip"
location = "westus2"
managementGroupId = "testMgIdRoot"
)

const (
moduleName = "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota"
moduleVersion = "v1.1.0-beta.1"
)

func createGroupQuota(groupName 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("f1d1800e-d38e-41f2-b63c-72d59ecaf9c0"),
},
},
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
}

func getGroupQuota(groupName string) {
clientFactory := createClientFactory()
ctx := context.Background()
res, err := clientFactory.NewGroupQuotasClient().Get(ctx, managementGroupId, groupName, 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.
_ = 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.GroupQuotasEntity = armquota.GroupQuotasEntity{
// Name: to.Ptr("groupquota1"),
// Type: to.Ptr("Microsoft.Quota/groupQuotas"),
// ID: to.Ptr("/providers/Microsoft.Management/managementGroups/E7EC67B3-7657-4966-BFFC-41EFD36BAA09/providers/Microsoft.Quota/groupQuotas/groupquota1"),
// Properties: &armquota.GroupQuotasEntityBase{
// AdditionalAttributes: &armquota.AdditionalAttributes{
// Environment: to.Ptr(armquota.EnvironmentTypeProduction),
// GroupID: &armquota.GroupingID{
// GroupingIDType: to.Ptr(armquota.GroupingIDTypeServiceTreeID),
// Value: to.Ptr("yourServiceTreeIdHere"),
// },
// },
// DisplayName: to.Ptr("GroupQuota1"),
// ProvisioningState: to.Ptr(armquota.RequestStateSucceeded),
// },
// }
}

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)
}
}

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"),
// },
// }
}

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

poller, err := clientFactory.NewGroupQuotaSubscriptionsClient().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)
}
}

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

var limitVal int64 = 64

groupQuotaLimitRequestBody := &armquota.GroupQuotaLimitsRequestClientBeginCreateOrUpdateOptions{
GroupQuotaRequest: &armquota.SubmittedResourceRequestStatus{
Properties: &armquota.SubmittedResourceRequestStatusProperties{
RequestedResource: &armquota.GroupQuotaRequestBase{
Properties: &armquota.GroupQuotaRequestBaseProperties{
Limit: &limitVal,
Region: to.Ptr("westus"),
},
},
},
},
}

poller, err := clientFactory.NewGroupQuotaLimitsRequestClient().BeginCreateOrUpdate(ctx, managementGroupId, groupName, "Microsoft.Compute", "cores", groupQuotaLimitRequestBody)
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.SubmittedResourceRequestStatus = armquota.SubmittedResourceRequestStatus{
// Name: to.Ptr("requestId1"),
// Type: to.Ptr("Microsoft.Quota/groupQuotas/groupQuotaLimitsRequests"),
// ID: to.Ptr("/providers/Microsoft.Management/managementGroups/E7EC67B3-7657-4966-BFFC-41EFD36BAA09/providers/Microsoft.Quota/groupQuotas/groupquota1/resourceProviders/Microsoft.Compute/groupQuotaLimitsRequests/requestId1"),
// Properties: &armquota.SubmittedResourceRequestStatusProperties{
// ProvisioningState: to.Ptr(armquota.RequestStateSucceeded),
// RequestSubmitTime: to.Ptr(func() time.Time { t, _ := time.Parse(time.RFC3339Nano, "2024-03-08T12:09:27.978Z"); return t}()),
// RequestedResource: &armquota.GroupQuotaRequestBase{
// Properties: &armquota.GroupQuotaRequestBaseProperties{
// Name: &armquota.GroupQuotaRequestBasePropertiesName{
// LocalizedValue: to.Ptr("Standard AV2 Family vCPUs"),
// Value: to.Ptr("standardav2family"),
// },
// Comments: to.Ptr("Contoso requires more quota."),
// Limit: to.Ptr[int64](100),
// Region: to.Ptr("westus"),
// },
// },
// },
// }
}

func main() {
//createGroupQuota("test-sdk-tejas-go", "testMgIdRoot")
//getGroupQuota("test-sdk-tejas-go")
//deleteGroupQuota("test-sdk-tejas-go-0")

//Subscription Functions
addSubscription("test-sdk-tejas-go")
//deleteSubscription("test-sdk-tejas-go")

//GroupQuotaLimit Request
//createGroupQuotaLimitRequest("test-sdk-tejas-go")
}

func createClientFactory() *armquota.ClientFactory {
//TODO: wrap in if statement for only corp tenant and only CLI
credOptions := azidentity.DefaultAzureCredentialOptions{
AdditionallyAllowedTenants: []string{"7be17286-b3ad-482d-ab84-4c772eebd529"},
}

cred, err := azidentity.NewDefaultAzureCredential(&credOptions)

if err != nil {
log.Fatalf("failed to obtain a credential: %v\n", err)
}

clientFactory, err := armquota.NewClientFactory("65a85478-2333-4bbd-981b-1a818c944faf", cred, getArmClientOptions())

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

return clientFactory
}

func getArmClientOptions() *armPolicy.ClientOptions {
return &armPolicy.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloud.Configuration{
ActiveDirectoryAuthorityHost: "https://login.microsoft.com/",
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
cloud.ResourceManager: {
Audience: "https://management.azure.com/",
Endpoint: "https://management.azure.com/",
},
},
},
},
AuxiliaryTenants: []string{},
DisableRPRegistration: false,
}
}