Last active
October 15, 2019 14:43
-
-
Save ragoragino/7f6488a34f0b2d7fda235afcb40fd74b to your computer and use it in GitHub Desktop.
Reproduce problem with https://github.com/terraform-providers/terraform-provider-azurerm/pull/4616
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb" | |
"github.com/Azure/go-autorest/autorest/azure" | |
"github.com/Azure/go-autorest/autorest/azure/auth" | |
) | |
const ( | |
resourceGroup = "resourceGroup" | |
subscriptionID = "subscriptionID" | |
accountName = "accountName" | |
azureAuthFile = `azure.auth` | |
) | |
func main() { | |
err := os.Setenv("AZURE_AUTH_LOCATION", azureAuthFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
authorizer, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint) | |
if err != nil { | |
log.Fatal(err) | |
} | |
client := documentdb.NewDatabaseAccountsClient(subscriptionID) | |
client.Authorizer = authorizer | |
ctx := context.Background() | |
testDbName1 := "test1" | |
err = createDbWithThroughput(ctx, client, testDbName1, 400) | |
if err != nil { | |
log.Fatalf("%+v", err) | |
} | |
resp, err := client.GetMongoDBDatabase(ctx, resourceGroup, accountName, testDbName1) | |
if err != nil { | |
log.Fatalf("Error making get request for Cosmos Mongo Database %s (Account %s): %+v", testDbName1, accountName, err) | |
} | |
log.Printf("Response from first DB: %+v", resp) | |
testDbName2 := "test2" | |
err = updateDbWithThrougphut(ctx, client, testDbName2, 400) | |
if err != nil { | |
log.Fatalf("%+v", err) | |
} | |
resp, err = client.GetMongoDBDatabase(ctx, resourceGroup, accountName, testDbName2) | |
if err != nil { | |
log.Fatalf("Error making get request for Cosmos Mongo Database %s (Account %s): %+v", testDbName2, accountName, err) | |
} | |
log.Printf("Response from second DB: %+v", resp) | |
} | |
func createDbWithThroughput(ctx context.Context, client documentdb.DatabaseAccountsClient, dbName string, throughput int) error { | |
throughputStr := strconv.Itoa(throughput) | |
db := documentdb.MongoDBDatabaseCreateUpdateParameters{ | |
MongoDBDatabaseCreateUpdateProperties: &documentdb.MongoDBDatabaseCreateUpdateProperties{ | |
Resource: &documentdb.MongoDBDatabaseResource{ | |
ID: &dbName, | |
}, | |
Options: map[string]*string{ | |
"x-ms-offer-throughput": &throughputStr, | |
}, | |
}, | |
} | |
future, err := client.CreateUpdateMongoDBDatabase(ctx, resourceGroup, accountName, dbName, db) | |
if err != nil { | |
return fmt.Errorf("Error issuing create/update request for Cosmos Mongo Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
return fmt.Errorf("Error waiting on create/update future for Cosmos Mongo Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
return nil | |
} | |
func updateDbWithThrougphut(ctx context.Context, client documentdb.DatabaseAccountsClient, dbName string, throughput int) error { | |
throughputInt32 := int32(throughput) | |
db := documentdb.MongoDBDatabaseCreateUpdateParameters{ | |
MongoDBDatabaseCreateUpdateProperties: &documentdb.MongoDBDatabaseCreateUpdateProperties{ | |
Resource: &documentdb.MongoDBDatabaseResource{ | |
ID: &dbName, | |
}, | |
Options: map[string]*string{}, | |
}, | |
} | |
future, err := client.CreateUpdateMongoDBDatabase(ctx, resourceGroup, accountName, dbName, db) | |
if err != nil { | |
return fmt.Errorf("Error issuing create/update request for Cosmos Mongo Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | |
return fmt.Errorf("Error waiting on create/update future for Cosmos Mongo Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
throughputParameters := documentdb.ThroughputUpdateParameters{ | |
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{ | |
Resource: &documentdb.ThroughputResource{ | |
Throughput: &throughputInt32, | |
}, | |
}, | |
} | |
throughputFuture, err := client.UpdateMongoDBDatabaseThroughput(ctx, resourceGroup, accountName, dbName, throughputParameters) | |
if err != nil { | |
return fmt.Errorf("Error setting Throughput for Cosmos MongoDB Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
if err = throughputFuture.WaitForCompletionRef(ctx, client.Client); err != nil { | |
return fmt.Errorf("Error waiting on ThroughputUpdate future for Cosmos Mongo Database %s (Account %s): %+v", dbName, accountName, err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment