Add Aptly REST API methods
This pathch adds methods to work with Aptly by Aptly REST API.
Change-Id: I69c7531ab1605a78971f97eba283c54eea98712f
diff --git a/src/com/mirantis/mk/Aptly.groovy b/src/com/mirantis/mk/Aptly.groovy
index ddbe4fd..c5aaca0 100644
--- a/src/com/mirantis/mk/Aptly.groovy
+++ b/src/com/mirantis/mk/Aptly.groovy
@@ -157,3 +157,130 @@
writeFile(file: "tmpFile", text: publish)
sh("aptly-publisher restore --url ${server} --restore-file tmpFile --components ${components} ${opts}")
}
+
+/**
+ * The function return name of the snapshot belongs to repo considering the prefix with storage by REST API.
+ *
+ * @param server URI the server to connect to aptly API
+ * @param destribution Destiribution of the repo which have to be found
+ * @param prefix Prefix of the repo including storage eg. prefix or s3:aptcdn:prefix
+ * @param component Component of the repo
+ *
+ * @return snapshot name
+ **/
+def getSnapshotByAPI(server, distribution, prefix, component) {
+ http = new com.mirantis.mk.Http()
+ def list_published = http.restGet(server, '/api/publish')
+ def storage
+ for (items in list_published) {
+ for (row in items) {
+ if (prefix.tokenize(':')[1]) {
+ storage = prefix.tokenize(':')[0] + ':' + prefix.tokenize(':')[1]
+ } else {
+ storage = ''
+ }
+ if (row.key == 'Distribution' && row.value == distribution && items['Prefix'] == prefix.tokenize(':').last() && items['Storage'] == storage) {
+ for (source in items['Sources']){
+ if (source['Component'] == component) {
+ if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
+ println ('Snapshot belongs to ' + distribution + '/' + prefix + ': ' + source['Name'])
+ }
+ return source['Name']
+ }
+ }
+ }
+ }
+ }
+ return false
+}
+
+/**
+ * Returns list of the packages matched to pattern and
+ * belonged to particular snapshot by REST API
+ *
+ * @param server URI of the server insluding port and protocol
+ * @param snapshotName Snapshot to check
+ * @param packagesList Pattern of the components to be compared
+ **/
+def snapshotPackagesByAPI(server, snapshotName, packagesList) {
+ http = new com.mirantis.mk.Http()
+ def pkgs = http.restGet(server, "/api/snapshots/${snapshotName}/packages")
+ def packages = []
+
+ for (package_pattern in packagesList.tokenize(',')) {
+ def pkg = pkgs.find { item -> item.contains(package_pattern) }
+ packages.add(pkg)
+ }
+
+ return packages
+}
+
+
+/**
+ * Creates snapshot of the repo or package refs by REST API
+ * @param server URI of the server insluding port and protocol
+ * @param repo Local repo name
+ * @param snapshotName Snapshot name is going to be created
+ * @param snapshotDescription Snapshot description
+ * @param packageRefs List of the packages are going to be included into the snapshot
+ **/
+def snapshotCreateByAPI(server, repo, snapshotName, snapshotDescription = null, packageRefs = null) {
+ http = new com.mirantis.mk.Http()
+ def data = [:]
+ data['Name'] = snapshotName
+ if (snapshotDescription) {
+ data['Description'] = snapshotDescription
+ } else {
+ data['Description'] = "Snapshot of ${repo} repo"
+ }
+ if (packageRefs) {
+ data['PackageRefs'] = packageRefs
+ http.restPost(server, '/api/snapshots', data)
+ } else {
+ http.restPost(server + "/api/repos/${repo}/snapshots", data)
+ }
+}
+
+/**
+ * Publishes the snapshot accodgin to distribution, components and prefix by REST API
+ * @param server URI of the server insluding port and protocol
+ * @param snapshotName Snapshot is going to be published
+ * @param distribution Distribution for the published repo
+ * @param components Component for the published repo
+ * @param prefix Prefix for thepubslidhed repo including storage
+ **/
+def snapshotPublishByAPI(server, snapshotName, distribution, components, prefix) {
+ http = new com.mirantis.mk.Http()
+ def source = [:]
+ source['Name'] = snapshotName
+ source['Component'] = components
+ def data = [:]
+ data['SourceKind'] = 'snapshot'
+ data['Sources'] = [source]
+ data['Architectures'] = ['amd64']
+ data['Distribution'] = distribution
+ return http.restPost(server, "/api/publish/${prefix}", data)
+}
+
+/**
+ * Unpublish Aptly repo by REST API
+ *
+ * @param aptlyServer Aptly connection object
+ * @param aptlyPrefix Aptly prefix where need to delete a repo
+ * @param aptlyRepo Aptly repo name
+ */
+def unpublishByAPI(aptlyServer, aptlyPrefix, aptlyRepo){
+ http = new com.mirantis.mk.Http()
+ http.restDelete(aptlyServer, "/api/publish/${aptlyPrefix}/${aptlyRepo}")
+}
+
+/**
+ * Delete Aptly repo by REST API
+ *
+ * @param aptlyServer Aptly connection object
+ * @param aptlyRepo Aptly repo name
+ */
+def deleteRepoByAPI(aptlyServer, aptlyRepo){
+ http = new com.mirantis.mk.Http()
+ http.restDelete(aptlyServer, "/api/repos/${aptlyRepo}")
+}
diff --git a/src/com/mirantis/mk/Http.groovy b/src/com/mirantis/mk/Http.groovy
index 7e22ca6..e14a523 100644
--- a/src/com/mirantis/mk/Http.groovy
+++ b/src/com/mirantis/mk/Http.groovy
@@ -184,6 +184,16 @@
}
/**
+ * Make DELETE request using Salt REST API and return parsed JSON
+ *
+ * @param master Salt connection object
+ * @param uri URI which will be appended to Salt server base URL
+ */
+def restDelete(master, uri, data = null) {
+ return restCall(master, uri, 'DELETE', data)
+}
+
+/**
* Set HTTP and HTTPS proxy for running JVM
* @param host HTTP proxy host
* @param port HTTP proxy port