Exemple #1
0
        public CreateCommand()
        {
            this.Name        = Constants.CreateName;
            this.Description = Constants.CreateDescription;

            // list command options
            CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();

            this.HelpOption();

            this.OnExecute(async() =>
            {
                // convert config file to CreatorConfig class
                YAMLReader yamlReader       = new YAMLReader();
                CreatorConfig creatorConfig = yamlReader.ConvertConfigYAMLToCreatorConfig(configFile.Value());

                // ensure required parameters have been passed in
                if (creatorConfig.outputLocation == null)
                {
                    throw new CommandParsingException(this, "Output location is required");
                }
                else if (creatorConfig.version == null)
                {
                    throw new CommandParsingException(this, "Version is required");
                }
                else if (creatorConfig.api == null)
                {
                    throw new CommandParsingException(this, "API configuration is required");
                }
                else if (creatorConfig.api.openApiSpec == null)
                {
                    throw new CommandParsingException(this, "Open API Spec is required");
                }
                else if (creatorConfig.api.suffix == null)
                {
                    throw new CommandParsingException(this, "API suffix is required");
                }
                else
                {
                    // required parameters have been supplied

                    // initialize helper classes
                    APITemplateCreator apiTemplateCreator = new APITemplateCreator();
                    ARMTemplateWriter armTemplateWriter   = new ARMTemplateWriter();

                    // create templates from provided configuration
                    APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(creatorConfig);

                    // write templates to outputLocation
                    armTemplateWriter.WriteAPITemplateToFile(apiTemplate, creatorConfig.outputLocation);
                    ColoredConsole.WriteLine("Templates written to output location");
                }
                return(0);
            });
        }
Exemple #2
0
        public CreateCommand()
        {
            this.Name        = Constants.CreateName;
            this.Description = Constants.CreateDescription;

            // list command options
            CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();

            this.HelpOption();

            this.OnExecute(async() =>
            {
                // convert config file to CreatorConfig class
                FileReader fileReader       = new FileReader();
                CreatorConfig creatorConfig = await fileReader.ConvertConfigYAMLToCreatorConfigAsync(configFile.Value());

                // ensure required parameters have been passed in
                if (creatorConfig.outputLocation == null)
                {
                    throw new CommandParsingException(this, "Output location is required");
                }
                else if (creatorConfig.version == null)
                {
                    throw new CommandParsingException(this, "Version is required");
                }
                else if (creatorConfig.apimServiceName == null)
                {
                    throw new CommandParsingException(this, "APIM service name is required");
                }
                else if (creatorConfig.api == null)
                {
                    throw new CommandParsingException(this, "API configuration is required");
                }
                else if (creatorConfig.api.openApiSpec == null)
                {
                    throw new CommandParsingException(this, "Open API Spec is required");
                }
                else if (creatorConfig.api.suffix == null)
                {
                    throw new CommandParsingException(this, "API suffix is required");
                }
                else if (creatorConfig.api.name == null)
                {
                    throw new CommandParsingException(this, "API name is required");
                }
                else if (creatorConfig.linked == true && creatorConfig.linkedTemplatesBaseUrl == null)
                {
                    throw new CommandParsingException(this, "LinkTemplatesBaseUrl is required for linked templates");
                }
                else
                {
                    // required parameters have been supplied

                    // initialize helper classes
                    FileWriter fileWriter           = new FileWriter();
                    TemplateCreator templateCreator = new TemplateCreator();
                    APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(templateCreator);
                    ProductAPITemplateCreator productAPITemplateCreator       = new ProductAPITemplateCreator();
                    PolicyTemplateCreator policyTemplateCreator         = new PolicyTemplateCreator(fileReader);
                    DiagnosticTemplateCreator diagnosticTemplateCreator = new DiagnosticTemplateCreator();
                    APITemplateCreator apiTemplateCreator       = new APITemplateCreator(fileReader, templateCreator, policyTemplateCreator, productAPITemplateCreator, diagnosticTemplateCreator);
                    MasterTemplateCreator masterTemplateCreator = new MasterTemplateCreator(templateCreator);

                    // create templates from provided configuration
                    Template apiVersionSetTemplate = creatorConfig.apiVersionSet != null ? apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig) : null;
                    Template initialAPITemplate    = await apiTemplateCreator.CreateInitialAPITemplateAsync(creatorConfig);
                    Template subsequentAPITemplate = apiTemplateCreator.CreateSubsequentAPITemplate(creatorConfig);
                    if (creatorConfig.linked == true)
                    {
                        CreatorFileNames creatorFileNames = fileWriter.GenerateCreatorLinkedFileNames();

                        // create linked master template
                        Template masterTemplate           = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, initialAPITemplate, subsequentAPITemplate, creatorFileNames);
                        Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);

                        // write templates to outputLocation
                        if (apiVersionSetTemplate != null)
                        {
                            fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.apiVersionSet));
                        }
                        fileWriter.WriteJSONToFile(initialAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.initialAPI));
                        fileWriter.WriteJSONToFile(subsequentAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.subsequentAPI));
                        fileWriter.WriteJSONToFile(masterTemplate, String.Concat(creatorConfig.outputLocation, "/master.template.json"));
                        fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, "/master.parameters.json"));
                    }
                    else
                    {
                        // create unlinked master template
                        Template initialMasterTemplate    = masterTemplateCreator.CreateInitialUnlinkedMasterTemplate(apiVersionSetTemplate, initialAPITemplate);
                        Template subsequentMasterTemplate = masterTemplateCreator.CreateSubsequentUnlinkedMasterTemplate(subsequentAPITemplate);
                        Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);

                        // write templates to outputLocation
                        fileWriter.WriteJSONToFile(initialMasterTemplate, String.Concat(creatorConfig.outputLocation, "/master1.template.json"));
                        fileWriter.WriteJSONToFile(subsequentMasterTemplate, String.Concat(creatorConfig.outputLocation, "/master2.template.json"));
                        fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, "/master.parameters.json"));
                    }

                    ColoredConsole.WriteLine("Templates written to output location");
                }
                return(0);
            });
        }
        public CreateCommand()
        {
            this.Name        = Constants.CreateName;
            this.Description = Constants.CreateDescription;

            // list command options
            CommandOption openAPISpecFile            = this.Option("--openAPISpecFile <openAPISpecFile>", "Open API spec file location", CommandOptionType.SingleValue);
            CommandOption openAPISpecURL             = this.Option("--openAPISpecURL <openAPISpecURL>", "Open API spec remote url", CommandOptionType.SingleValue);
            CommandOption outputLocation             = this.Option("--outputLocation <outputLocation>", "Template output location", CommandOptionType.SingleValue).IsRequired();
            CommandOption xmlPolicyFile              = this.Option("--xmlPolicyFile <xmlPolicyFile>", "XML policy file location", CommandOptionType.SingleValue);
            CommandOption xmlPolicyURL               = this.Option("--xmlPolicyURL <xmlPolicyURL>", "XML policy remote url", CommandOptionType.SingleValue);
            CommandOption linked                     = this.Option("--linked <linked>", "Creates linked templates versus inlined into a single file", CommandOptionType.SingleValue);
            CommandOption path                       = this.Option("--path <path>", "API path", CommandOptionType.SingleValue);
            CommandOption apiRevision                = this.Option("--apiRevision <apiRevision>", "API revision", CommandOptionType.SingleValue);
            CommandOption apiRevisionDescription     = this.Option("--apiRevisionDescription <apiVersionSetId>", "Description of the API revision", CommandOptionType.SingleValue);
            CommandOption apiVersion                 = this.Option("--apiVersion <apiVersion>", "API version", CommandOptionType.SingleValue);
            CommandOption apiVersionDescription      = this.Option("--apiVersionDescription <apiVersionSetId>", "Description of the API version", CommandOptionType.SingleValue);
            CommandOption apiVersionSetFile          = this.Option("--apiVersionSetFile <apiVersionSetId>", "YAML file with object that follows the ApiVersionSetContractDetails object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#ApiVersionSetContractDetails", CommandOptionType.SingleValue);
            CommandOption authenticationSettingsFile = this.Option("--authenticationSettingsFile <apiVersionSetId>", "YAML file with object that follows the AuthenticationSettingsContract object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#AuthenticationSettingsContract", CommandOptionType.SingleValue);
            CommandOption apiVersionSetId            = this.Option("--apiVersionSetId <apiVersionSetId>", "API version set id", CommandOptionType.SingleValue);
            CommandOption productIds                 = this.Option("--productIds <productIds>", "Product ids to associate the API with", CommandOptionType.MultipleValue);

            this.HelpOption();

            this.OnExecute(async() =>
            {
                // ensure required parameters have been passed in
                if ((openAPISpecFile.HasValue() || openAPISpecURL.HasValue()) && outputLocation.HasValue())
                {
                    // convert command options to CLIArguments class
                    CLICreatorArguments cliArguments = new CLICreatorArguments()
                    {
                        openAPISpecFile            = openAPISpecFile.Value(),
                        openAPISpecURL             = openAPISpecURL.Value(),
                        outputLocation             = outputLocation.Value(),
                        xmlPolicyFile              = xmlPolicyFile.Value(),
                        xmlPolicyURL               = xmlPolicyURL.Value(),
                        linked                     = linked.HasValue(),
                        path                       = path.Value(),
                        apiRevision                = apiRevision.Value(),
                        apiRevisionDescription     = apiRevisionDescription.Value(),
                        apiVersion                 = apiVersion.Value(),
                        apiVersionDescription      = apiVersionDescription.Value(),
                        apiVersionSetFile          = apiVersionSetFile.Value(),
                        apiVersionSetId            = apiVersionSetId.Value(),
                        authenticationSettingsFile = authenticationSettingsFile.Value(),
                        productIds                 = productIds.Values
                    };

                    if (apiVersionSetFile.HasValue() && AttemptAPIVersionSetConversion(cliArguments) != null)
                    {
                        // unable to convert version set argument into object, would cause failure down the line
                        ColoredConsole.Error.WriteLine("Incorrect apiVersionSet object structure");
                        return(0);
                    }
                    else if (authenticationSettingsFile.HasValue() && AttemptAuthenticationSettingsConversion(cliArguments) != null)
                    {
                        // unable to convert version set argument into object, would cause failure down the line
                        ColoredConsole.Error.WriteLine("Incorrect authenticationSettings object structure");
                        return(0);
                    }
                    else
                    {
                        // required parameters have been supplied and versionSet has correct object structure

                        // initialize helper classes
                        OpenAPISpecReader openAPISpecReader   = new OpenAPISpecReader();
                        ARMTemplateWriter armTemplateWriter   = new ARMTemplateWriter();
                        APITemplateCreator apiTemplateCreator = new APITemplateCreator();
                        TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();

                        // create OpenApiDocument from Open API spec file
                        OpenApiDocument doc = new OpenApiDocument();
                        if (cliArguments.openAPISpecFile != null)
                        {
                            doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
                        }
                        else
                        {
                            doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
                        }

                        // create templates from OpenApiDocument
                        APITemplate apiTemplate         = await apiTemplateCreator.CreateAPITemplateAsync(doc, cliArguments);
                        List <TagTemplate> tagTemplates = tagTemplateCreator.CreateTagTemplates(doc);
                        List <TagDescriptionTemplate> tagDescriptionTemplates = tagTemplateCreator.CreateTagDescriptionTemplates(doc);

                        // write templates to outputLocation
                        armTemplateWriter.WriteAPITemplateToFile(apiTemplate, cliArguments.outputLocation);
                        armTemplateWriter.WriteTagTemplatesToFile(tagTemplates, cliArguments.outputLocation);
                        armTemplateWriter.WriteTagDescriptionTemplatesToFile(tagDescriptionTemplates, cliArguments.outputLocation);
                        ColoredConsole.WriteLine("Templates written to output location");
                    }
                }
                else if (!outputLocation.HasValue())
                {
                    ColoredConsole.Error.WriteLine("Output location is required");
                }
                else if (!(openAPISpecFile.HasValue() || openAPISpecURL.HasValue()))
                {
                    ColoredConsole.Error.WriteLine("Open API spec file or remote url is required");
                }
                ;
                return(0);
            });
        }