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

implement the download feature for the cli #495

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions PO039-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2019-2024 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

const testID = "PO039",
assert = require("assert"),
fs = require("fs"),
path = require("path"),
bl = require("../../lib/package/bundleLinter.js"),
plugin = require(bl.resolvePlugin(testID)),
debug = require("debug")("apigeelint:" + testID),
Policy = require("../../lib/package/Policy.js"),
Dom = require("@xmldom/xmldom").DOMParser,
rootDir = path.resolve(__dirname, "../fixtures/resources/PO039");

const test = (suffix, cb) => {
const filename = `ML-test-${suffix}.xml`;
it(`should correctly process ${filename}`, () => {
const fqfname = path.join(rootDir, filename),
policyXml = fs.readFileSync(fqfname, "utf-8"),
doc = new Dom().parseFromString(policyXml),
p = new Policy(rootDir, filename, this, doc);

p.getElement = () => doc.documentElement;

//plugin.onBundle({ profile: "apigee" });

plugin.onPolicy(p, (e, foundIssues) => {
assert.equal(e, undefined, "should be undefined");
cb(p, foundIssues);
});
});
};

describe(`${testID} - MessageLogging RessourceType element`, () => {
// test all the valid cases
fs.readdirSync(rootDir)
.map((shortFileName) => {
let m = shortFileName.match("^.+-(valid.+)\\.xml$");
if (m) {
return m[1];
}
})
.filter((suffix) => suffix)
.forEach((suffix) => {
test(suffix, (p, foundIssues) => {
const messages = p.getReport().messages;
assert.ok(messages, "messages undefined");
debug(messages);
assert.equal(foundIssues, false);
});
});

test("invalid1", (p, foundIssues) => {
assert.equal(foundIssues, true);
const messages = p.getReport().messages;
assert.ok(messages, "messages undefined");
debug(messages);
assert.equal(messages.length, 1, "unexpected number of messages");
assert.ok(messages[0].message, "did not find message 0");
assert.equal(
messages[0].message,
"The value 'gce_instance' should not be used here. ResourceType should be 'api'",
);
});

test("invalid2", (p, foundIssues) => {
assert.equal(foundIssues, true);
const messages = p.getReport().messages;
assert.ok(messages, "messages undefined");
debug(messages);
assert.equal(messages.length, 1, "unexpected number of messages");
assert.ok(messages[0].message, "did not find message 0");
assert.equal(
messages[0].message,
"The value 'apigee.googleapis.com/Environment' should not be used here. ResourceType should be 'api'",
);
});

test("invalid3", (p, foundIssues) => {
assert.equal(foundIssues, true);
const messages = p.getReport().messages;
assert.ok(messages, "messages undefined");
debug(messages);
assert.equal(messages.length, 2, "unexpected number of messages");
assert.ok(messages[0].message, "did not find message 0");
assert.equal(messages[0].message, "Unsupported element 'NotKey'");
assert.equal(
messages[1].message,
"Label is missing a required Element: Key.",
);
});
});
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Usage: apigeelint [options]
Options:
-V, --version output the version number
-s, --path <path> Path of the proxy to analyze
-d, --download [value] Download the API proxy or sharedflow to analyze. Exclusive of -s / --path. Example: org:ORG,API:proxyname or org:ORG,sf:SHAREDFLOWNAME
-f, --formatter [value] Specify formatters (default: json.js)
-w, --write [value] file path to write results
-e, --excluded [value] The comma separated list of tests to exclude (default: none)
Expand Down Expand Up @@ -106,10 +107,48 @@ archive. This tool also can read and analyze these zipped bundles:
apigeelint -f table.js -s path/to/your/apiproxy.zip
```

The tool will unzip the bundle to a temporary directory, perform the analysis,
The tool will unzip the bundle into a temporary directory, perform the analysis,
and then remove the temporary directory.


### Basic usage: downloading a proxy bundle to analyze

You can ask apigeelint to export an API Proxy or Sharedflow bundle from Apigee,
and analyze the resulting zip archive. This connects to apigee.googleapis.com to
perform the export, which means it will work only with Apigee X or hybrid.

```
# to download and then analyze a proxy bundle
apigeelint -f table.js -d org:your-org-name,api:name-of-your-api-proxy

# to download and then analyze a sharedflow bundle
apigeelint -f table.js -d org:your-org-name,sf:name-of-your-shared-flow
```

With this invocation, the tool will:
- obtain a token using the `gcloud auth print-access-token` command
- use the token to inquire the latest revision of the proxy or sharedflow
- use the token to download the bundle for the latest revision
- unzip the bundle into a temporary directory
- perform the lint analysis
- render the result
- and then remove the temporary directory

If you do not have the [`gcloud` command line
tool](https://cloud.google.com/sdk/gcloud) installed, and available on your
path, this will fail.


You can also specify a token you have obtained previously:

```
apigeelint -f table.js -d org:your-org-name,api:name-of-your-api-proxy,token:ACCESS_TOKEN_HERE
```

In this case, apigeelint does not try to use `gcloud` to obtain an access token.



### Using External Plugins:
```
apigeelint -x ./externalPlugins -s path/to/your/apiproxy -f table.js
Expand Down
Loading
Loading