Exemplo n.º 1
0
        protected async Task <ManagementGroupPolicyDefinitionResource> CreatePolicyDefinitionAtMgmtGroup(ManagementGroupResource mgmtGroup, string policyDefinitionName)
        {
            PolicyDefinitionData input = ConstructPolicyDefinitionData(policyDefinitionName);
            ArmOperation <ManagementGroupPolicyDefinitionResource> lro = await mgmtGroup.GetManagementGroupPolicyDefinitions().CreateOrUpdateAsync(WaitUntil.Completed, policyDefinitionName, input);

            return(lro.Value);
        }
Exemplo n.º 2
0
        protected async Task <SubscriptionPolicyDefinitionResource> CreatePolicyDefinitionAtSubscription(SubscriptionResource subscription, string policyDefinitionName)
        {
            PolicyDefinitionData input = ConstructPolicyDefinitionData(policyDefinitionName);
            ArmOperation <SubscriptionPolicyDefinitionResource> lro = await subscription.GetSubscriptionPolicyDefinitions().CreateOrUpdateAsync(WaitUntil.Completed, policyDefinitionName, input);

            return(lro.Value);
        }
Exemplo n.º 3
0
        internal static PolicyDefinitionListResult DeserializePolicyDefinitionListResult(JsonElement element)
        {
            Optional <IReadOnlyList <PolicyDefinitionData> > value = default;
            Optional <string> nextLink = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("value"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <PolicyDefinitionData> array = new List <PolicyDefinitionData>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(PolicyDefinitionData.DeserializePolicyDefinitionData(item));
                    }
                    value = array;
                    continue;
                }
                if (property.NameEquals("nextLink"))
                {
                    nextLink = property.Value.GetString();
                    continue;
                }
            }
            return(new PolicyDefinitionListResult(Optional.ToList(value), nextLink.Value));
        }
Exemplo n.º 4
0
        protected async Task <ManagementGroupPolicyDefinition> CreatePolicyDefinitionAtMgmtGroup(ManagementGroup mgmtGroup, string policyDefinitionName)
        {
            PolicyDefinitionData input = ConstructPolicyDefinitionData(policyDefinitionName);
            ArmOperation <ManagementGroupPolicyDefinition> lro = await mgmtGroup.GetManagementGroupPolicyDefinitions().CreateOrUpdateAsync(true, policyDefinitionName, input);

            return(lro.Value);
        }
        protected async Task <SubscriptionPolicyDefinition> CreatePolicyDefinitionAtSubscription(Subscription subscription, string policyDefinitionName)
        {
            PolicyDefinitionData input = ConstructPolicyDefinitionData(policyDefinitionName);
            SubscriptionPolicyDefinitionCreateOrUpdateOperation lro = await subscription.GetSubscriptionPolicyDefinitions().CreateOrUpdateAsync(true, policyDefinitionName, input);

            return(lro.Value);
        }
Exemplo n.º 6
0
        /**
         * Azure PolicyDefinition sample for managing policy definitions -
         * - Create a policy definition
         * - Create another policy definition
         * - List policy definitions
         * - Delete policy definitions.
         */
        public static async Task RunSample(ArmClient client)
        {
            var policyDefinitionName1 = "pd1";
            var policyDefinitionName2 = "pd2";
            var indexedMode           = "Indexed";
            var policyRuleJson        = "{\"if\":{\"not\":{\"field\":\"location\",\"in\":[\"northeurope\",\"westeurope\"]}},\"then\":{\"effect\":\"deny\"}}";

            var subscription = await client.GetDefaultSubscriptionAsync();

            try
            {
                //=============================================================
                // Create policy definition.

                Console.WriteLine($"Creating a policy definition with name: {policyDefinitionName1}");

                var policyDefinitionData = new PolicyDefinitionData
                {
                    PolicyRule = BinaryData.FromString(policyRuleJson),
                    PolicyType = PolicyType.Custom,
                };
                // this operation returns an ArmOperation which is used to track the status of an operation that might take a long time
                // by passing `WaitUntil.Completed` will automatically make the function to wait for the completion of this operation
                var lro = await subscription.GetSubscriptionPolicyDefinitions().CreateOrUpdateAsync(WaitUntil.Completed, policyDefinitionName1, policyDefinitionData);

                var policyDefinition = lro.Value;

                Console.WriteLine($"Policy definition created: {policyDefinition.Id}");

                //=============================================================
                // Create another policy definition.

                Console.WriteLine($"Creating another policy definition with name: {policyDefinitionName2}");

                policyDefinitionData = new PolicyDefinitionData
                {
                    PolicyRule = BinaryData.FromString(policyRuleJson),
                    PolicyType = PolicyType.Custom,
                    Mode       = indexedMode,
                };
                lro = await subscription.GetSubscriptionPolicyDefinitions().CreateOrUpdateAsync(WaitUntil.Completed, policyDefinitionName2, policyDefinitionData);

                var policyDefinition2 = lro.Value;

                Console.WriteLine($"Policy definition created: {policyDefinition2.Id}");

                //=============================================================
                // List policy definitions.

                Console.WriteLine("Listing all policy definitions: ");

                await foreach (var pDefinition in subscription.GetSubscriptionPolicyDefinitions())
                {
                    Console.WriteLine($"Policy definition: {pDefinition.Id}");
                }

                //=============================================================
                // Delete policy definitions.

                Console.WriteLine($"Deleting policy definition: {policyDefinitionName1}");

                await policyDefinition.DeleteAsync(WaitUntil.Completed);

                Console.WriteLine($"Deleted policy definition: {policyDefinitionName1}");

                Console.WriteLine($"Deleting policy definition: {policyDefinitionName2}");

                await policyDefinition2.DeleteAsync(WaitUntil.Completed);

                Console.WriteLine($"Deleted policy definition: {policyDefinitionName2}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }