Ejemplo n.º 1
0
        public object GetDynamicParameters()
        {
            if (!string.IsNullOrEmpty(GalleryTemplateIdentity) &&
                !GalleryTemplateIdentity.Equals(galleryTemplateName, StringComparison.OrdinalIgnoreCase))
            {
                galleryTemplateName = GalleryTemplateIdentity;
                dynamicParameters   = GalleryTemplatesClient.GetTemplateParametersFromGallery(
                    GalleryTemplateIdentity,
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }
            else if (!string.IsNullOrEmpty(TemplateFile) &&
                     !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
            {
                templateFile      = TemplateFile;
                dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                    this.TryResolvePath(TemplateFile),
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }
            else if (!string.IsNullOrEmpty(TemplateUri) &&
                     !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
            {
                templateUri       = TemplateUri;
                dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                    TemplateUri,
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }

            return(dynamicParameters);
        }
Ejemplo n.º 2
0
 protected virtual void SaveFields(XmlTextWriter output)
 {
     output.WriteAttributeString("templatename", Name);
     output.WriteAttributeString("templateid", TemplateUri.ItemId.ToString());
     output.WriteAttributeString("templateuri", TemplateUri.ToString());
     output.WriteAttributeString("classname", ClassName);
 }
        public object GetDynamicParameters()
        {
            if (!string.IsNullOrEmpty(GalleryTemplateIdentity) &&
                !GalleryTemplateIdentity.Equals(galleryTemplateName, StringComparison.OrdinalIgnoreCase))
            {
                galleryTemplateName = GalleryTemplateIdentity;
                try
                {
                    dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromGallery(
                        GalleryTemplateIdentity,
                        TemplateParameterObject,
                        this.TryResolvePath(TemplateParameterFile),
                        MyInvocation.MyCommand.Parameters.Keys.ToArray());
                }
                catch (CloudException)
                {
                    throw new ArgumentException(string.Format(Resources.UnableToFindGallery, GalleryTemplateIdentity));
                }
            }
            else if (!string.IsNullOrEmpty(TemplateFile) &&
                     !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    templateFile      = TemplateFile;
                    dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                        this.TryResolvePath(TemplateFile),
                        TemplateParameterObject,
                        this.TryResolvePath(TemplateParameterFile),
                        MyInvocation.MyCommand.Parameters.Keys.ToArray());
                }
                catch
                {
                    throw new ArgumentException(string.Format(Resources.FailedToParseProperty, "TemplateFile", TemplateFile));
                }
            }
            else if (!string.IsNullOrEmpty(TemplateUri) &&
                     !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    templateUri       = TemplateUri;
                    dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                        TemplateUri,
                        TemplateParameterObject,
                        this.TryResolvePath(TemplateParameterFile),
                        MyInvocation.MyCommand.Parameters.Keys.ToArray());
                }
                catch
                {
                    throw new ArgumentException(string.Format(Resources.FailedToParseProperty, "TemplateUri", TemplateUri));
                }
            }

            return(dynamicParameters);
        }
Ejemplo n.º 4
0
        private void EnsureTemplates()
        {
            if (templates != null)
            {
                return;
            }

            var mappings = TemplateMappings.GetMappingsForBaseTemplate(TemplateUri.ToString());

            templates =
                mappings == null
                    ? new List <TemplateReference>(0)
                    : mappings.Select(t => new TemplateReference {
                Version = t.Version, Template = CreateLazyTemplate(new Uri(t.TemplateUri))
            }).ToList();
        }
        public virtual object GetDynamicParameters()
        {
            if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
            {
                // Resolve the static parameter names for this cmdlet:
                string[] staticParameterNames = this.GetStaticParameterNames();

                if (TemplateObject != null && TemplateObject != templateObject)
                {
                    templateObject = TemplateObject;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateFile) &&
                         !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
                {
                    templateFile = TemplateFile;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateUri) &&
                         !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
                {
                    if (string.IsNullOrEmpty(protectedTemplateUri))
                    {
                        templateUri = TemplateUri;
                    }
                    else
                    {
                        templateUri = protectedTemplateUri;
                    }
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateUri,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateUri,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateSpecId) &&
                         !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase))
                {
                    templateSpecId = TemplateSpecId;
                    ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId);
                    if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException("No version found in Resource ID");
                    }

                    if (!string.IsNullOrEmpty(resourceIdentifier.Subscription) &&
                        TemplateSpecsClient.SubscriptionId != resourceIdentifier.Subscription)
                    {
                        // The template spec is in a different subscription than our default
                        // context. Force the client to use that subscription:
                        TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription;
                    }

                    var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get(
                        ResourceIdUtility.GetResourceGroupName(templateSpecId),
                        ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0],
                        resourceIdentifier.ResourceName);

                    if (!(templateSpecVersion.Template is JObject))
                    {
                        throw new InvalidOperationException("Unexpected type."); // Sanity check
                    }

                    JObject templateObj = (JObject)templateSpecVersion.Template;

                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
            }

            RegisterDynamicParameters(dynamicParameters);

            return(dynamicParameters);
        }
Ejemplo n.º 6
0
        public object GetDynamicParameters()
        {
            if (!string.IsNullOrEmpty(GalleryTemplateIdentity))
            {
                List <PSGalleryItem> galleryItems = new List <PSGalleryItem>();
                try
                {
                    galleryItems = GalleryTemplatesClient.FilterGalleryTemplates(new FilterGalleryTemplatesOptions()
                    {
                        Identity = GalleryTemplateIdentity
                    });
                }
                catch (CloudException)
                {
                    // we could not find a template with that identity
                }

                if (galleryItems.Count == 0)
                {
                    galleryItems = GalleryTemplatesClient.FilterGalleryTemplates(new FilterGalleryTemplatesOptions()
                    {
                        ApplicationName = GalleryTemplateIdentity, AllVersions = false
                    });
                    if (galleryItems == null || galleryItems.Count == 0)
                    {
                        throw new ArgumentException(string.Format(Properties.Resources.InvalidTemplateIdentity, GalleryTemplateIdentity));
                    }

                    GalleryTemplateIdentity = galleryItems[0].Identity;
                }
            }

            if (!string.IsNullOrEmpty(GalleryTemplateIdentity) &&
                !GalleryTemplateIdentity.Equals(galleryTemplateName, StringComparison.OrdinalIgnoreCase))
            {
                galleryTemplateName = GalleryTemplateIdentity;
                dynamicParameters   = GalleryTemplatesClient.GetTemplateParametersFromGallery(
                    GalleryTemplateIdentity,
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }
            else if (!string.IsNullOrEmpty(TemplateFile) &&
                     !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
            {
                templateFile      = TemplateFile;
                dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                    this.TryResolvePath(TemplateFile),
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }
            else if (!string.IsNullOrEmpty(TemplateUri) &&
                     !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
            {
                templateUri       = TemplateUri;
                dynamicParameters = GalleryTemplatesClient.GetTemplateParametersFromFile(
                    TemplateUri,
                    TemplateParameterObject,
                    this.TryResolvePath(TemplateParameterFile),
                    MyInvocation.MyCommand.Parameters.Keys.ToArray());
            }

            return(dynamicParameters);
        }
        public virtual object GetDynamicParameters()
        {
            if (BicepUtility.IsBicepFile(TemplateFile))
            {
                BuildAndUseBicepTemplate();
            }

            if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
            {
                // Resolve the static parameter names for this cmdlet:
                string[] staticParameterNames = this.GetStaticParameterNames();

                if (TemplateObject != null && TemplateObject != templateObject)
                {
                    templateObject = TemplateObject;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateFile) &&
                         !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
                {
                    templateFile = TemplateFile;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateUri) &&
                         !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
                {
                    if (string.IsNullOrEmpty(protectedTemplateUri))
                    {
                        templateUri = TemplateUri;
                    }
                    else
                    {
                        templateUri = protectedTemplateUri;
                    }
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateUri,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateUri,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateSpecId) &&
                         !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase))
                {
                    templateSpecId = TemplateSpecId;
                    ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId);
                    if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException("No version found in Resource ID");
                    }

                    if (!string.IsNullOrEmpty(resourceIdentifier.Subscription) &&
                        TemplateSpecsClient.SubscriptionId != resourceIdentifier.Subscription)
                    {
                        // The template spec is in a different subscription than our default
                        // context. Force the client to use that subscription:
                        TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription;
                    }
                    JObject templateObj = (JObject)null;
                    try
                    {
                        var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get(
                            ResourceIdUtility.GetResourceGroupName(templateSpecId),
                            ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0],
                            resourceIdentifier.ResourceName);

                        if (!(templateSpecVersion.Template is JObject))
                        {
                            throw new InvalidOperationException("Unexpected type."); // Sanity check
                        }
                        templateObj = (JObject)templateSpecVersion.Template;
                    }
                    catch (TemplateSpecsErrorException e)
                    {
                        //If the templateSpec resourceID is pointing to a non existant resource
                        if (e.Response.StatusCode.Equals(HttpStatusCode.NotFound))
                        {
                            //By returning null, we are introducing parity in the way templateURI and templateSpecId are validated. Gives a cleaner error message in line with the error message for invalid templateURI
                            return(null);
                        }
                        //Throw for any other error that is not due to a 404 for the template resource.
                        throw;
                    }

                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            staticParameterNames);
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            staticParameterNames);
                    }
                }
            }

            RegisterDynamicParameters(dynamicParameters);

            return(dynamicParameters);
        }
Ejemplo n.º 8
0
        public object GetDynamicParameters()
        {
            if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
            {
                if (TemplateObject != null && TemplateObject != templateObject)
                {
                    templateObject = TemplateObject;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateFile) &&
                         !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
                {
                    templateFile = TemplateFile;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateUri) &&
                         !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
                {
                    templateUri = TemplateUri;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateUri,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateUri,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateSpecId) &&
                         !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase))
                {
                    templateSpecId = TemplateSpecId;
                    ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId);
                    if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException("No version found in Resource ID");
                    }

                    var templateSpecVersion = TemplateSpecsSdkClient.GetTemplateSpec(
                        ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0],
                        ResourceIdUtility.GetResourceGroupName(templateSpecId),
                        resourceIdentifier.ResourceName).Versions.Single();

                    var templateObj = JObject.Parse(templateSpecVersion.Template);

                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            templateObj,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
            }

            RegisterDynamicParameters(dynamicParameters);

            return(dynamicParameters);
        }
        public object GetDynamicParameters()
        {
            if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
            {
                if (TemplateObject != null && TemplateObject != templateObject)
                {
                    templateObject = TemplateObject;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateObject,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateFile) &&
                         !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase))
                {
                    templateFile = TemplateFile;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            this.ResolvePath(TemplateFile),
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
                else if (!string.IsNullOrEmpty(TemplateUri) &&
                         !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase))
                {
                    templateUri = TemplateUri;
                    if (string.IsNullOrEmpty(TemplateParameterUri))
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateUri,
                            TemplateParameterObject,
                            this.ResolvePath(TemplateParameterFile),
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                    else
                    {
                        dynamicParameters = TemplateUtility.GetTemplateParametersFromFile(
                            TemplateUri,
                            TemplateParameterObject,
                            TemplateParameterUri,
                            MyInvocation.MyCommand.Parameters.Keys.ToArray());
                    }
                }
            }

            RegisterDynamicParameters(dynamicParameters);

            return(dynamicParameters);
        }