예제 #1
0
        /// <summary>
        /// Create a PSBluprintAssignment object from an Assignment model.
        /// </summary>
        /// <param name="assignment">Assignment object from which to create the PSBlueprintAssignment.</param>
        /// <param name="subscriptionId">ID of the subscription the assignment is associated with.</param>
        /// <returns>A new PSBlueprintAssignment object.</returns>
        internal static PSBlueprintAssignment FromAssignment(Assignment assignment, string scope)
        {
            var psAssignment = new PSBlueprintAssignment
            {
                Name     = assignment.Name,
                Id       = assignment.Id,
                Type     = assignment.Type,
                Location = assignment.Location,
                Scope    = scope,
                Identity = new PSManagedServiceIdentity
                {
                    PrincipalId            = assignment.Identity.PrincipalId,
                    TenantId               = assignment.Identity.TenantId,
                    Type                   = assignment.Type,
                    UserAssignedIdentities = new Dictionary <string, PSUserAssignedIdentity>()
                },
                DisplayName       = assignment.DisplayName,
                Description       = assignment.Description,
                BlueprintId       = assignment.BlueprintId,
                ProvisioningState = PSAssignmentProvisioningState.Unknown,
                Status            = new PSAssignmentStatus(),
                Locks             = new PSAssignmentLockSettings {
                    Mode = PSLockMode.None
                },
                Parameters     = new Dictionary <string, PSParameterValueBase>(),
                ResourceGroups = new Dictionary <string, PSResourceGroupValue>()
            };

            psAssignment.Status.TimeCreated = assignment.Status.TimeCreated;

            psAssignment.Status.LastModified = assignment.Status.LastModified;

            if (Enum.TryParse(assignment.ProvisioningState, true, out PSAssignmentProvisioningState state))
            {
                psAssignment.ProvisioningState = state;
            }
            else
            {
                psAssignment.ProvisioningState = PSAssignmentProvisioningState.Unknown;
            }

            if (Enum.TryParse(assignment.Locks.Mode, true, out PSLockMode lockMode))
            {
                psAssignment.Locks.Mode = lockMode;
            }
            else
            {
                psAssignment.Locks.Mode = PSLockMode.None;
            }

            foreach (var item in assignment.Parameters)
            {
                PSParameterValueBase parameter = GetAssignmentParameters(item);
                psAssignment.Parameters.Add(item.Key, parameter);
            }

            foreach (var item in assignment.ResourceGroups)
            {
                psAssignment.ResourceGroups.Add(item.Key,
                                                new PSResourceGroupValue {
                    Name = item.Value.Name, Location = item.Value.Location
                });
            }

            if (assignment.Identity.UserAssignedIdentities != null)
            {
                foreach (var item in assignment.Identity.UserAssignedIdentities)
                {
                    psAssignment.Identity.UserAssignedIdentities.Add(item.Key,
                                                                     new PSUserAssignedIdentity {
                        ClientId = item.Value.ClientId, PrincipalId = item.Value.PrincipalId
                    });
                }
            }

            return(psAssignment);
        }
        /// <summary>
        /// Create a PSBluprintAssignment object from an Assignment model.
        /// </summary>
        /// <param name="assignment">Assignment object from which to create the PSBlueprintAssignment.</param>
        /// <param name="subscriptionId">ID of the subscription the assignment is associated with.</param>
        /// <returns>A new PSBlueprintAssignment object.</returns>
        internal static PSBlueprintAssignment FromAssignment(Assignment assignment, string scope)
        {
            var psAssignment = new PSBlueprintAssignment
            {
                Name     = assignment.Name,
                Id       = assignment.Id,
                Type     = assignment.Type,
                Location = assignment.Location,
                Scope    = scope,
                Identity = new PSManagedServiceIdentity
                {
                    PrincipalId            = assignment.Identity.PrincipalId,
                    TenantId               = assignment.Identity.TenantId,
                    Type                   = assignment.Type,
                    UserAssignedIdentities = new Dictionary <string, PSUserAssignedIdentity>()
                },
                DisplayName       = assignment.DisplayName,
                Description       = assignment.Description,
                BlueprintId       = assignment.BlueprintId,
                ProvisioningState = PSAssignmentProvisioningState.Unknown,
                Status            = new PSAssignmentStatus(),
                Locks             = new PSAssignmentLockSettings {
                    Mode = PSLockMode.None
                },
                Parameters     = new Dictionary <string, PSParameterValueBase>(),
                ResourceGroups = new Dictionary <string, PSResourceGroupValue>()
            };

            if (DateTime.TryParse(assignment.Status.TimeCreated, out DateTime timeCreated))
            {
                psAssignment.Status.TimeCreated = timeCreated;
            }
            else
            {
                psAssignment.Status.TimeCreated = null;
            }

            if (DateTime.TryParse(assignment.Status.LastModified, out DateTime lastModified))
            {
                psAssignment.Status.LastModified = lastModified;
            }
            else
            {
                psAssignment.Status.LastModified = null;
            }

            if (Enum.TryParse(assignment.ProvisioningState, true, out PSAssignmentProvisioningState state))
            {
                psAssignment.ProvisioningState = state;
            }
            else
            {
                psAssignment.ProvisioningState = PSAssignmentProvisioningState.Unknown;
            }

            if (Enum.TryParse(assignment.Locks.Mode, true, out PSLockMode lockMode))
            {
                psAssignment.Locks.Mode = lockMode;
            }
            else
            {
                psAssignment.Locks.Mode = PSLockMode.None;
            }

            foreach (var item in assignment.Parameters)
            {
                var paramObj = item.Value as ParameterValue;

                if (paramObj == null || !paramObj.GetType().Equals(typeof(ParameterValue)))
                {
                    throw new NotSupportedException(string.Format(Resources.SecureStringsNotSupported, psAssignment.Name));
                }

                psAssignment.Parameters.Add(item.Key, new PSParameterValue {
                    Description = paramObj.Description, Value = paramObj.Value
                });
            }

            foreach (var item in assignment.ResourceGroups)
            {
                psAssignment.ResourceGroups.Add(item.Key,
                                                new PSResourceGroupValue {
                    Name = item.Value.Name, Location = item.Value.Location
                });
            }

            if (assignment.Identity.UserAssignedIdentities != null)
            {
                foreach (var item in assignment.Identity.UserAssignedIdentities)
                {
                    psAssignment.Identity.UserAssignedIdentities.Add(item.Key,
                                                                     new PSUserAssignedIdentity {
                        ClientId = item.Value.ClientId, PrincipalId = item.Value.PrincipalId
                    });
                }
            }

            return(psAssignment);
        }