Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Azure REST samples support #178

Merged
merged 31 commits into from
Sep 21, 2018
Merged

Adding Azure REST samples support #178

merged 31 commits into from
Sep 21, 2018

Conversation

zikalino
Copy link

Summary

Issue Type

  • New Feature

package.json Outdated
@@ -31,7 +31,8 @@
"onCommand:vscode-ansible.playbook-in-docker",
"onCommand:vscode-ansible.playbook-in-localansible",
"onCommand:vscode-ansible.ssh",
"onCommand:vscode-ansible.sync-folder-ssh"
"onCommand:vscode-ansible.sync-folder-ssh",
"onCommand:vscode-ansible.rest-api-samples"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to rest-module-samples. which is related to ansible

@@ -5,7 +5,7 @@ import * as vscode from 'vscode';
export class PlaybookManager {
constructor() {
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove it

}

public displayMenu() {
this.createRestApiCall();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need a wrapper of displayMenu()?

// - query list of api groups (compute, etc...)
// - user selects api group
public createRestApiCall() {
let __this = this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need private __this?

public selectOperation(path: string) {

let operations = this.queryAll(path);
let __this = this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of private this


examples.forEach(function(s, i, a) {
items.push({
'label': apiVersion + ' - Sample: ' + s.split('/').pop(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we discussed, remove the word 'Sample"here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sorry, forgot....

pm.insertTask(playbook);
} else {
let playbook = swaggerHandler.generateRestApiTasks(selection['path'], selection['method'], selection['example']);
pm.insertTask(playbook);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move pm.insertTask() out of if/else

});
}

public getSpecificationLocation(cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest use async func instead of callback to make coding style consistent

}

public getSpecificationLocation(cb) {
let config = vscode.workspace.getConfiguration('ansible');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's utility in utiltities.js: getCodeConfiguration

cb(config.get('AzureRestAPISpecificationPath'));
} else {

this._outputChannel.append('\nConnecting to Cloud Shell.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong comments


this._outputChannel.append('\nConnecting to Cloud Shell.');
this._outputChannel.show();
const progress = utilities.delayedInterval(() => { this._outputChannel.append('.') }, 500);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

progress should at end of below log message, otherwise you'll ...............Getting Azurexxxxx

src/restSamples.ts Outdated Show resolved Hide resolved
for (var path in swagger.paths) {
for (var method in swagger.paths[path]) {
// add only if there are examples
if (swagger.paths[path][method]['x-ms-examples'] != undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!swagger.paths[path][method]['x-ms-examples'] , to cover both undefined, null and empty check

this.queryDirectory(specLocation + '/specification', false, "", cb);
}

private queryApiGroup(path, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 2 function names confusing.. why not use queryDirectory and queryApiGroup?

src/swagger.ts Outdated
method = method.toLowerCase();
playbook += //"# https://docs.microsoft.com/en-us/rest/api/" + area + "/" + resource + "/" + internalMethod + '\r';
"- name: Call REST API - " + this.swagger.paths[path][method]['operationId'] + "\r" +
"\t" + ((method == 'get') ? "azure_rm_resource_facts" : "azure_rm_resource") + ":\r" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls replace \t with whitespace array constants

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

playbookManager is replacing tabs with spaces but it doesnt' really matter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs /whitespace is matter. :(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course they matter, and they are replaced later :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a public api. you really want your user do tab replace by themselves?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe they are replaced automatically when inserting sample using code in playbookManager. I could recheck it again...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but anyway, I replaced them with spaces....

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit.insert(new vscode.Position(0, 0)….. in vscode works in such a way that all inserted tabs are replaced with spaces according to settings appropriate for current editor

src/swagger.ts Outdated Show resolved Hide resolved
@@ -25,11 +25,11 @@ export class PlaybookManager {

public insertTask(task: string) {
let __this = this;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls do code formatting by 'shift+alt+f' to remove those whitesapces.

if (vscode.window.activeTextEditor.document.getText() == "") {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, the sample will be only inserted into utiltiled-1 doc, what if user want to insert into existing doc, focus position?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fi existing yaml document is open, new sample will be inserted at the end

src/restSamples.ts Outdated Show resolved Hide resolved
let operations = this.queryAll(path);
let items = [];

for (var key in operations) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operations null/undefined check?

let home: string = path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], 'azure-rest-api-specs');
clone("https://github.com/Azure/azure-rest-api-specs.git", home, null, (result) => {
progress.cancel();
if (result == undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use check if (!result) for undefined, null, empty check

src/restSamples.ts Outdated Show resolved Hide resolved
private queryDirectory(path: string, files: boolean, ext: string, cb) {
// just use filesystem
try {
let dirEntries = fs.readdirSync(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check path exists before read?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's why try is here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps there should be a message and we could clear the configuration if not there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you'd like the caller handle when path not exists?

src/swagger.ts Outdated
method = method.toLowerCase();
playbook += //"# https://docs.microsoft.com/en-us/rest/api/" + area + "/" + resource + "/" + internalMethod + '\r';
"- name: Call REST API - " + this.swagger.paths[path][method]['operationId'] + "\r" +
"\t" + ((method == 'get') ? "azure_rm_resource_facts" : "azure_rm_resource") + ":\r" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs /whitespace is matter. :(

let specLocation = await this.getSpecificationLocation();
__this.queryDirectory(specLocation + '/specification', false, "", function (groups) {
if (groups != null) {
vscode.window.showQuickPick(groups).then(selection => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is async function already, pls use await vscode.window.showQuickPick()


this._outputChannel.append("Getting Azure REST API specifications.");
let clone = require('git-clone');
let home: string = path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], 'azure-rest-api-specs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under .vscode to make it hide?

this._outputChannel.append("Getting Azure REST API specifications.");
let clone = require('git-clone');
let home: string = path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], 'azure-rest-api-specs');
clone("https://github.com/Azure/azure-rest-api-specs.git", home, null, (result) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async function, pls use await instead of callback

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this library only supports callback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then pls remove async on function declaration

let operationId: string = swagger.paths[path][method].operationId;
let description: string = swagger.paths[path][method].description;

if (!description || description == '') description = "Description not available";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not show this to user, by empty

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just show empty string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

private queryDirectory(path: string, files: boolean, ext: string, cb) {
// just use filesystem
try {
let dirEntries = fs.readdirSync(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you'd like the caller handle when path not exists?

src/swagger.ts Outdated

method = method.toLowerCase();
playbook += //"# https://docs.microsoft.com/en-us/rest/api/" + area + "/" + resource + "/" + internalMethod + '\r';
"- name: Call REST API - " + this.swagger.paths[path][method]['operationId'] + "\r" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample for Rest API... ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with sample link here?

README.md Outdated
@@ -139,6 +140,24 @@ Enable syntax highlighting by adding `files.associations` in `settings.json`, to
```
There's notification message at right side of status bar.

## Samples for azure_rm_resource (preview)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preview

README.md Outdated
@@ -139,6 +140,24 @@ Enable syntax highlighting by adding `files.associations` in `settings.json`, to
```
There's notification message at right side of status bar.

## Samples for azure_rm_resource (preview)

This option provides easy an access to the samples available from Azure REST API Specifications.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option provides sample code snippets for [azure_rm_resource](link here) module, those samples are from Azure Rest API spec.

README.md Outdated
This option provides easy an access to the samples available from Azure REST API Specifications.

To use, press **F1** and then select **Ansible: Samples for azure_rm_resource (PREVIEW)** option.
You will be first asked for API group, then required operation. After selecting operation a submenu will be displayed where you can select particular API version and sample. Extension will create appropriate playbook based on **azure_rm_resource** or **azure_rm_resource_facts** module.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

submenu -> pickup list

README.md Outdated

Note that all the samples generated by the extension are based on REST API samples from this repository:

https://github.com/Azure/azure-rest-api-specs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you meet error when using those samples, please file issues at xxxx/issues.

README.md Outdated Show resolved Hide resolved
this._outputChannel.append("Getting Azure REST API specifications.");
let clone = require('git-clone');
let home: string = path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], 'azure-rest-api-specs');
clone("https://github.com/Azure/azure-rest-api-specs.git", home, null, (result) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then pls remove async on function declaration

}
}

private async queryAll(path: string): Promise<{}> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also remove async if there's no async operation in function

return operations;
}

private async queryApiGroup(path): Promise<string[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
}

private async queryDirectory(path: string, files: boolean, ext: string): Promise<string[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no async calls inside

@zikalino
Copy link
Author

tested vsix on windows and mac.....

@zikalino zikalino merged commit be9ef5b into master Sep 21, 2018
@zikalino zikalino deleted the rest-samples branch September 21, 2018 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants