Exemplo n.º 1
0
        public override void ExecuteCmdlet()
        {
            string zoneName              = null;
            string resourceGroupName     = null;
            PSPrivateDnsRecordSet result = null;

            if (RecordType == RecordType.SOA)
            {
                throw new System.ArgumentException(ProjectResources.Error_AddRecordSOA);
            }

            switch (this.ParameterSetName)
            {
            case FieldsParameterSetName:
                zoneName          = this.ZoneName;
                resourceGroupName = this.ResourceGroupName;
                break;

            case ObjectParameterSetName:
                zoneName          = this.Zone.Name;
                resourceGroupName = this.Zone.ResourceGroupName;
                break;

            case ResourceParameterSetName:
                PrivateDnsUtils.GetResourceGroupNameAndZoneNameFromResourceId(this.ParentResourceId, out resourceGroupName, out zoneName);
                break;
            }

            if (zoneName != null && this.Name.EndsWith(zoneName))
            {
                this.WriteWarning(string.Format(ProjectResources.Error_RecordSetNameEndsWithZoneName, this.Name, zoneName));
            }

            zoneName = TrimTrailingDotInZoneName(zoneName);

            ConfirmAction(
                ProjectResources.Progress_CreatingRecordSet,
                this.Name,
                () =>
            {
                result = this.PrivateDnsClient.CreatePrivateDnsRecordSet(
                    zoneName,
                    resourceGroupName,
                    this.Name,
                    this.Ttl,
                    this.RecordType,
                    this.Metadata,
                    this.Overwrite,
                    this.PrivateDnsRecord);

                if (result != null)
                {
                    WriteVerbose(ProjectResources.Success);
                    WriteVerbose(string.Format(ProjectResources.Success_NewRecordSet, this.Name, zoneName, this.RecordType));
                    WriteVerbose(string.Format(ProjectResources.Success_RecordSetFqdn, this.Name, zoneName, this.RecordType));
                }

                WriteObject(result);
            });
        }
        public override void ExecuteCmdlet()
        {
            var deleted = false;
            PSPrivateDnsRecordSet recordSetToDelete = null;

            switch (this.ParameterSetName)
            {
            case FieldsParameterSetName:
            case MixedParameterSetName:
            {
                if (this.Name.EndsWith("."))
                {
                    this.Name = this.Name.TrimEnd('.');
                    this.WriteWarning(string.Format(ProjectResources.Progress_ModifyingRecordSetNameTrimDot, this.Name));
                }

                recordSetToDelete = new PSPrivateDnsRecordSet
                {
                    Name              = this.Name,
                    Etag              = null,
                    RecordType        = this.RecordType,
                    ResourceGroupName = (this.ParameterSetName == FieldsParameterSetName) ? this.ResourceGroupName : this.Zone.ResourceGroupName,
                    ZoneName          = (this.ParameterSetName == FieldsParameterSetName) ? this.ZoneName : this.Zone.Name,
                };
                break;
            }

            case ObjectParameterSetName when(string.IsNullOrWhiteSpace(this.RecordSet.Etag) || this.RecordSet.Etag == "*") && !this.Overwrite.IsPresent:
                throw new PSArgumentException(string.Format(ProjectResources.Error_EtagNotSpecified, typeof(PSPrivateDnsRecordSet).Name));

            case ObjectParameterSetName:
                recordSetToDelete = this.RecordSet;
                break;

            case ResourceParameterSetName:
            {
                PrivateDnsUtils.GetResourceGroupNameZoneNameRecordNameAndRecordTypeFromResourceId(ResourceId, out var resourceGroupName, out var zoneName, out var recordName, out var recordType);
                recordSetToDelete = new PSPrivateDnsRecordSet
                {
                    Name              = recordName,
                    Etag              = null,
                    RecordType        = (RecordType)Enum.Parse(typeof(RecordType), recordType, true),
                    ResourceGroupName = resourceGroupName,
                    ZoneName          = zoneName,
                };
                break;
            }
            }

            if (recordSetToDelete?.ZoneName != null)
            {
                recordSetToDelete.ZoneName = TrimTrailingDotInZoneName(recordSetToDelete.ZoneName);
            }

            var overwrite = this.Overwrite.IsPresent || this.ParameterSetName != ObjectParameterSetName;

            ConfirmAction(
                ProjectResources.Progress_RemovingRecordSet,
                this.Name,
                () =>
            {
                deleted = PrivateDnsClient.DeletePrivateDnsRecordSet(recordSetToDelete, overwrite);
                if (deleted)
                {
                    WriteVerbose(ProjectResources.Success);
                    WriteVerbose(ProjectResources.Success_RemoveRecordSet);
                }

                if (this.PassThru)
                {
                    WriteObject(deleted);
                }
            });
        }