Zig
Zig
Azure Create Storage Account
See more Azure Storage Accounts Examples
Demonstrates how to create an Azure storage account.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// Load an OAuth2 access token previously fetched by this example: Get Azure OAuth2 Access Token
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/azureToken.json");
// Assuming success..
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
std.debug.print("AuthToken: {s}\n", .{try http.getAuthToken(alloc)});
http.setAccept("application/json");
// Create the following JSON:
// {
// "sku": {
// "name": "Standard_GRS"
// },
// "kind": "StorageV2",
// "location": "eastus2",
// }
// Use this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
const json_request_body = try chilkat.JsonObject.init();
defer json_request_body.deinit();
json_request_body.updateString("sku.name", "Standard_GRS") catch {};
json_request_body.updateString("kind", "StorageV2") catch {};
json_request_body.updateString("location", "eastus2") catch {};
const url = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2018-02-01";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("PUT", url, json_request_body, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
if (resp.getStatusCode() >= 300) {
std.debug.print("Failed.\n", .{});
return;
}
// Successful requests to create a new account return a 202 status code with an empty response body. The storage account is created asynchronously.
// If the account already exists or is being provisioned, the request response has a 200 return code with the configuration of the existing storage account in the response body.
if (resp.getStatusCode() == 202) {
std.debug.print("Azure-AsyncOperation: {s}\n", .{try resp.getHeaderField(alloc, "Azure-AsyncOperation")});
}
if (resp.getStatusCode() == 200) {
// Parse a response like this:
// {
// "sku": {
// "name": "Standard_GRS",
// "tier": "Standard"
// },
// "kind": "StorageV2",
// "id": "/subscriptions/6c42643b-ebef-45f0-b917-b3583b84a57f/resourceGroups/gChilkat/providers/Microsoft.Storage/storageAccounts/chilkatsoftware",
// "name": "chilkatsoftware",
// "type": "Microsoft.Storage/storageAccounts",
// "location": "eastus2",
// "tags": {},
// "properties": {
// "networkAcls": {
// "bypass": "AzureServices",
// "virtualNetworkRules": [
// ],
// "ipRules": [
// ],
// "defaultAction": "Allow"
// },
// "supportsHttpsTrafficOnly": false,
// "encryption": {
// "services": {
// "file": {
// "enabled": true,
// "lastEnabledTime": "2019-05-14T22:18:33.2246670Z"
// },
// "blob": {
// "enabled": true,
// "lastEnabledTime": "2019-05-14T22:18:33.2246670Z"
// }
// },
// "keySource": "Microsoft.Storage"
// },
// "accessTier": "Hot",
// "provisioningState": "Succeeded",
//
// "creationTime": "2019-05-14T22:18:33.1309165Z",
// "primaryEndpoints": {
// "dfs": "https://chilkatsoftware.dfs.core.windows.net/",
// "web": "https://chilkatsoftware.z20.web.core.windows.net/",
// "blob": "https://chilkatsoftware.blob.core.windows.net/",
// "queue": "https://chilkatsoftware.queue.core.windows.net/",
// "table": "https://chilkatsoftware.table.core.windows.net/",
// "file": "https://chilkatsoftware.file.core.windows.net/"
// },
// "primaryLocation": "eastus2",
// "statusOfPrimary": "available",
// "secondaryLocation": "centralus",
// "statusOfSecondary": "available"
// }
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
_ = try json.stringOf(alloc, "sku.name");
_ = try json.stringOf(alloc, "sku.tier");
_ = try json.stringOf(alloc, "kind");
_ = try json.stringOf(alloc, "id");
_ = try json.stringOf(alloc, "name");
_ = try json.stringOf(alloc, "type");
_ = try json.stringOf(alloc, "location");
_ = try json.stringOf(alloc, "properties.networkAcls.bypass");
_ = try json.stringOf(alloc, "properties.networkAcls.defaultAction");
_ = json.boolOf("properties.supportsHttpsTrafficOnly");
_ = json.boolOf("properties.encryption.services.file.enabled");
_ = try json.stringOf(alloc, "properties.encryption.services.file.lastEnabledTime");
_ = json.boolOf("properties.encryption.services.blob.enabled");
_ = try json.stringOf(alloc, "properties.encryption.services.blob.lastEnabledTime");
_ = try json.stringOf(alloc, "properties.encryption.keySource");
_ = try json.stringOf(alloc, "properties.accessTier");
_ = try json.stringOf(alloc, "properties.provisioningState");
_ = try json.stringOf(alloc, "properties.creationTime");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.dfs");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.web");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.blob");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.queue");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.table");
_ = try json.stringOf(alloc, "properties.primaryEndpoints.file");
_ = try json.stringOf(alloc, "properties.primaryLocation");
_ = try json.stringOf(alloc, "properties.statusOfPrimary");
_ = try json.stringOf(alloc, "properties.secondaryLocation");
_ = try json.stringOf(alloc, "properties.statusOfSecondary");
}
std.debug.print("Success.\n", .{});
}