Esempio n. 1
0
        public async Task CreateOrUpdateAtSubscription()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(subscription, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(subscription, policyAssignment, policyExemptionName);

            Assert.AreEqual(policyExemptionName, policyExemption.Data.Name);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await subscription.GetPolicyExemptions().CreateOrUpdateAsync(true, null, policyExemption.Data));
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await subscription.GetPolicyExemptions().CreateOrUpdateAsync(true, policyExemptionName, null));
        }
Esempio n. 2
0
        public async Task CreateOrUpdateAtMgmtGroup()
        {
            //This test uses a pre-created management group.
            ManagementGroup mgmtGroup = await GetCreatedManagementGroup();

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(mgmtGroup, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(mgmtGroup, policyAssignment, policyExemptionName);

            Assert.AreEqual(policyExemptionName, policyExemption.Data.Name);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await mgmtGroup.GetPolicyExemptions().CreateOrUpdateAsync(true, null, policyExemption.Data));
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await mgmtGroup.GetPolicyExemptions().CreateOrUpdateAsync(true, policyExemptionName, null));
        }
Esempio n. 3
0
        public async Task CreateOrUpdateAtResourceGroup()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string        rgName = Recording.GenerateAssetName("testRg-");
            ResourceGroup rg     = await CreateResourceGroup(subscription, rgName);

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(rg, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(rg, policyAssignment, policyExemptionName);

            Assert.AreEqual(policyExemptionName, policyExemption.Data.Name);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await rg.GetPolicyExemptions().CreateOrUpdateAsync(WaitUntil.Completed, null, policyExemption.Data));
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await rg.GetPolicyExemptions().CreateOrUpdateAsync(WaitUntil.Completed, policyExemptionName, null));
        }
Esempio n. 4
0
        public async Task Get()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string        rgName = Recording.GenerateAssetName("testRg-");
            ResourceGroup rg     = await CreateResourceGroup(subscription, rgName);

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(rg, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(rg, policyAssignment, policyExemptionName);

            PolicyExemption getPolicyExemption = await rg.GetPolicyExemptions().GetAsync(policyExemptionName);

            AssertValidPolicyExemption(policyExemption, getPolicyExemption);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await rg.GetPolicyExemptions().GetAsync(null));
        }
Esempio n. 5
0
        public async Task Delete()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string        rgName = Recording.GenerateAssetName("testRg-");
            ResourceGroup rg     = await CreateResourceGroup(subscription, rgName);

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(rg, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(rg, policyAssignment, policyExemptionName);

            await policyExemption.DeleteAsync(true);

            var ex = Assert.ThrowsAsync <RequestFailedException>(async() => await policyExemption.GetAsync());

            Assert.AreEqual(404, ex.Status);
        }
        /// <summary>
        /// Constructs the resource
        /// </summary>
        private JToken GetResource()
        {
            var policyExemption = new PolicyExemption
            {
                Name       = this.Name,
                Properties = new PolicyExemptionProperties
                {
                    DisplayName                  = this.DisplayName ?? null,
                    Description                  = this.Description ?? null,
                    ExemptionCategory            = this.ExemptionCategory,
                    PolicyAssignmentId           = this.PolicyAssignment.PolicyAssignmentId,
                    PolicyDefinitionReferenceIds = this.PolicyDefinitionReferenceId,
                    ExpiresOn = this.ExpiresOn?.ToUniversalTime(),
                    Metadata  = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)),
                }
            };

            return(policyExemption.ToJToken());
        }
Esempio n. 7
0
        public async Task CreateOrUpdateAtResource()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string        rgName = Recording.GenerateAssetName("testRg-");
            ResourceGroup rg     = await CreateResourceGroup(subscription, rgName);

            string          vnName = Recording.GenerateAssetName("testVn-");
            GenericResource vn     = await CreateGenericVirtualNetwork(subscription, rg, vnName);

            string           policyAssignmentName = Recording.GenerateAssetName("polAssign-");
            PolicyAssignment policyAssignment     = await CreatePolicyAssignment(vn, policyAssignmentName);

            string          policyExemptionName = Recording.GenerateAssetName("polExemp-");
            PolicyExemption policyExemption     = await CreatePolicyExemption(vn, policyAssignment, policyExemptionName);

            Assert.AreEqual(policyExemptionName, policyExemption.Data.Name);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await vn.GetPolicyExemptions().CreateOrUpdateAsync(true, null, policyExemption.Data));
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await vn.GetPolicyExemptions().CreateOrUpdateAsync(true, policyExemptionName, null));
        }
Esempio n. 8
0
        /// <summary>
        /// Constructs the resource
        /// </summary>
        private JToken GetResource(string resourceId, string apiVersion)
        {
            var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource();

            // get incoming object properties if present
            JObject inputMetadata = null;

            if (this.InputObject != null)
            {
                var newProperties = this.InputObject.Properties?.ToJToken();
                inputMetadata = newProperties["metadata"] as JObject;
            }

            DateTime?existingExpiration = null;

            if (DateTime.TryParse(resource.Properties["expiresOn"]?.ToString(), out var expiresOn))
            {
                existingExpiration = expiresOn;
            }

            var parameterMetadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata));
            var policyExemption   = new PolicyExemption
            {
                Name       = this.Name ?? this.InputObject?.Name ?? resource.Name,
                Properties = new PolicyExemptionProperties
                {
                    DisplayName                  = this.DisplayName ?? this.InputObject?.Properties?.DisplayName ?? resource.Properties["displayName"]?.ToString(),
                    Description                  = this.Description ?? this.InputObject?.Properties?.Description ?? resource.Properties["description"]?.ToString(),
                    ExemptionCategory            = this.ExemptionCategory ?? this.InputObject?.Properties?.ExemptionCategory ?? resource.Properties["exemptionCategory"]?.ToString(),
                    PolicyAssignmentId           = resource.Properties["policyAssignmentId"]?.ToString(),
                    PolicyDefinitionReferenceIds = this.PolicyDefinitionReferenceId ?? this.InputObject?.Properties?.PolicyDefinitionReferenceIds ?? resource.Properties["policyDefinitionReferenceIds"]?.ToString()?.Split(','),
                    ExpiresOn = this.ClearExpiration.IsPresent ? null : this.ExpiresOn?.ToUniversalTime() ?? this.InputObject?.Properties?.ExpiresOn ?? existingExpiration,
                    Metadata  = parameterMetadata ?? inputMetadata ?? resource.Properties["metadata"] as JObject,
                }
            };

            return(policyExemption.ToJToken());
        }
Esempio n. 9
0
 private void AssertValidPolicyExemption(PolicyExemption model, PolicyExemption getResult)
 {
     Assert.AreEqual(model.Data.Name, getResult.Data.Name);
     Assert.AreEqual(model.Data.Id, getResult.Data.Id);
     Assert.AreEqual(model.Data.Type, getResult.Data.Type);
     if (model.Data.SystemData != null || getResult.Data.SystemData != null)
     {
         Assert.NotNull(model.Data.SystemData);
         Assert.NotNull(getResult.Data.SystemData);
         Assert.AreEqual(model.Data.SystemData.CreatedAt, getResult.Data.SystemData.CreatedAt);
         Assert.AreEqual(model.Data.SystemData.CreatedBy, getResult.Data.SystemData.CreatedBy);
         Assert.AreEqual(model.Data.SystemData.CreatedByType, getResult.Data.SystemData.CreatedByType);
         Assert.AreEqual(model.Data.SystemData.LastModifiedAt, getResult.Data.SystemData.LastModifiedAt);
         Assert.AreEqual(model.Data.SystemData.LastModifiedBy, getResult.Data.SystemData.LastModifiedBy);
         Assert.AreEqual(model.Data.SystemData.LastModifiedByType, getResult.Data.SystemData.LastModifiedByType);
     }
     Assert.AreEqual(model.Data.PolicyAssignmentId, getResult.Data.PolicyAssignmentId);
     Assert.AreEqual(model.Data.PolicyDefinitionReferenceIds, getResult.Data.PolicyDefinitionReferenceIds);
     Assert.AreEqual(model.Data.ExemptionCategory, getResult.Data.ExemptionCategory);
     Assert.AreEqual(model.Data.ExpiresOn, getResult.Data.ExpiresOn);
     Assert.AreEqual(model.Data.DisplayName, getResult.Data.DisplayName);
     Assert.AreEqual(model.Data.Description, getResult.Data.Description);
     Assert.AreEqual(model.Data.Metadata, getResult.Data.Metadata);
 }
 public virtual PolicyExemption GetPolicyExemption(ResourceIdentifier id)
 {
     PolicyExemption.ValidateResourceId(id);
     return(new PolicyExemption(this, id));
 }