コード例 #1
0
        public override void ExecuteCmdlet()
        {
            try
            {
                switch (ParameterSetName)
                {
                case GetTemplateSpecByNameParameterSet:
                    WriteObject(
                        TemplateSpecsSdkClient.GetTemplateSpec(Name, ResourceGroupName, Version)
                        );
                    break;

                case GetTemplateSpecByIdParameterSet:
                    WriteObject(
                        TemplateSpecsSdkClient.GetTemplateSpec(
                            ResourceIdUtility.GetResourceName(this.ResourceId),
                            ResourceIdUtility.GetResourceGroupName(this.ResourceId),
                            Version
                            )
                        );
                    break;

                case ListTemplateSpecsParameterSet:
                    var templateSpecs = !string.IsNullOrEmpty(ResourceGroupName)
                            ? TemplateSpecsSdkClient.ListTemplateSpecsByResourceGroup(ResourceGroupName)
                            : TemplateSpecsSdkClient.ListTemplateSpecsBySubscription();

                    var templateSpecListItems = templateSpecs
                                                .Select(ts => PSTemplateSpecListItem.FromTemplateSpec(ts))
                                                .GroupBy(ts => ts.Id).Select(g => g.First()) // Required due to current backend bug returning duplicates
                                                .OrderBy(ts => ts.ResourceGroupName)
                                                .ThenBy(ts => ts.Name)
                                                .Distinct()
                                                .ToList();

                    WriteObject(templateSpecListItems);
                    break;

                default:
                    throw new PSInvalidOperationException();
                }
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }
コード例 #2
0
        public override void ExecuteCmdlet()
        {
            try
            {
                // TODO: Update the following to use ??= when we upgrade to C# 8.0
                if (ResourceGroupName == null)
                {
                    ResourceGroupName = ResourceIdUtility.GetResourceGroupName(this.ResourceId);
                }

                if (Name == null)
                {
                    Name = ResourceIdUtility.GetResourceName(this.ResourceId);
                }

                if (Version != null)
                {
                    // This is a version specific update...

                    PackagedTemplate packagedTemplate;

                    switch (ParameterSetName)
                    {
                    case UpdateVersionByIdFromJsonFileParameterSet:
                    case UpdateVersionByNameFromJsonFileParameterSet:
                        string filePath = this.TryResolvePath(TemplateFile);
                        if (!File.Exists(filePath))
                        {
                            throw new PSInvalidOperationException(
                                      string.Format(ProjectResources.InvalidFilePath, TemplateFile)
                                      );
                        }

                        packagedTemplate = TemplateSpecPackagingEngine.Pack(filePath);
                        break;

                    case UpdateVersionByIdFromJsonParameterSet:
                    case UpdateVersionByNameFromJsonParameterSet:
                        JObject parsedTemplate;
                        try
                        {
                            parsedTemplate = JObject.Parse(TemplateJson);
                        }
                        catch
                        {
                            // Check if the user may have inadvertantly passed a file path using "-TemplateJson"
                            // instead of using "-TemplateFile". If they did, display a warning message. Note we
                            // do not currently automatically switch to using a file in this case because if the
                            // JSON string is coming from an external script/source not authored directly by the
                            // caller it could result in a sensitive template being PUT unintentionally:

                            try
                            {
                                string asFilePath = this.TryResolvePath(TemplateJson);
                                if (File.Exists(asFilePath))
                                {
                                    WriteWarning(
                                        $"'{TemplateJson}' was found to exist as a file. Did you mean to use '-TemplateFile' instead?"
                                        );
                                }
                            }
                            catch
                            {
                                // Subsequent failure in the file existence check... Ignore it
                            }

                            throw;
                        }

                        // When we're provided with a raw JSON string for the template we don't
                        // do any special packaging... (ie: we don't pack artifacts because there
                        // is no well known root path):

                        packagedTemplate = new PackagedTemplate
                        {
                            RootTemplate = JObject.Parse(TemplateJson),
                            Artifacts    = new TemplateSpecArtifact[0]
                        };
                        break;

                    default:
                        throw new PSNotSupportedException();
                    }

                    if (!ShouldProcess($"{Name}/versions/{Version}", "Create or Update"))
                    {
                        return;
                    }

                    var templateSpecVersion = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpecVersion(
                        ResourceGroupName,
                        Name,
                        Version,
                        Location,
                        packagedTemplate,
                        templateSpecDescription: Description,
                        templateSpecDisplayName: DisplayName,
                        versionDescription: VersionDescription,
                        templateSpecTags: Tag, // Note: Only applied if template spec doesn't exist
                        versionTags: Tag
                        );

                    WriteObject(templateSpecVersion);
                }
                else
                {
                    // This is an update to the root template spec only:

                    if (!ShouldProcess(Name, "Create or Update"))
                    {
                        return;
                    }

                    var templateSpec = TemplateSpecsSdkClient.CreateOrUpdateTemplateSpec(
                        ResourceGroupName,
                        Name,
                        Location,
                        templateSpecDescription: Description,
                        templateSpecDisplayName: DisplayName,
                        tags: Tag
                        );

                    // As the root template spec is a seperate resource type, it won't contain version
                    // details. To provide more information to the user, let's get the template spec
                    // with all of its version details:

                    var templateSpecWithVersions =
                        TemplateSpecsSdkClient.GetTemplateSpec(templateSpec.Name, templateSpec.ResourceGroupName);

                    WriteObject(templateSpecWithVersions);
                }
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }