Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
feat: Duplicate an api to create a new version of the API
Browse files Browse the repository at this point in the history
  • Loading branch information
aelamrani committed Oct 18, 2019
1 parent 11326ed commit d73fcc7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/management/api/portal/general/apiPortal.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class ApiPortalController {
});
}
}
})
});
}

canAskForReview(): boolean {
Expand Down Expand Up @@ -464,7 +464,23 @@ class ApiPortalController {
this.NotificationService.show(`Review has been asked for API ${this.api.name}`);
});
}
})
});
}

showDuplicateDialog() {
this.$mdDialog.show({
controller: 'DialogApiDuplicateController',
controllerAs: '$ctrl',
template: require('./dialog/apiDuplicate.dialog.html'),
clickOutsideToClose: true,
locals: {
api: this.$scope.$parent.apiCtrl.api
}
}).then((api) => {
if (api) {
this.$state.go('management.apis.detail.portal.general', {apiId: api.id});
}
});
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/management/api/portal/general/apiPortal.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ <h2>General</h2>
<ng-md-icon icon="file_download"></ng-md-icon>
Import
</md-button>


<md-button permission permission-only="['api-definition-u', 'management-api-c']" aria-label="Duplicate API" ng-click="portalCtrl.showDuplicateDialog()">
<ng-md-icon icon="content_copy"></ng-md-icon>
Duplicate
</md-button>
</div>
</div>
</div>
Expand Down
62 changes: 62 additions & 0 deletions src/management/api/portal/general/dialog/apiDuplicate.dialog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!--
Copyright (C) 2015 The Gravitee team (http://gravitee.io)
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
http://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.
-->
<md-dialog>
<form name="formApiDefinition" ng-submit="$ctrl.duplicate()" novalidate>
<md-dialog-content layout-padding>
<h4>What do you want to duplicate?</h4>
<div layout="column">
<div layout="row">
<md-input-container layout="row" flex>
<label>Context path</label>
<input type="text" ng-model="$ctrl.contextPath" name="contextPath" ng-change="$ctrl.onContextPathChanged()"
minlength="3" ng-pattern="/^\/[\/.a-zA-Z0-9-_]+$/" placeholder="{{$ctrl.contextPathPlaceholder}}" required>
<div ng-messages="$ctrl.contextPath.$error">
<div ng-message="required">Context path is required.</div>
<div ng-message="minlength">Context path has to be more than 3 characters long.</div>
<div ng-message="pattern">Context path is not valid (must start with a '/' and must contain any letter, capitalize letter, number, dash or underscore)</div>
</div>
<ng-md-icon icon="close" style="fill: red" ng-if="$ctrl.contextPathInvalid === true"></ng-md-icon>
<ng-md-icon icon="check" style="fill: green" ng-if="$ctrl.contextPathInvalid === false"></ng-md-icon>
</md-input-container>

<md-input-container flex="30">
<label>Version</label>
<input ng-model="$ctrl.version" type="text" maxlength="32" name="version" placeholder="{{$ctrl.versionPlaceholder}}">
<div class="hint" ng-if="$ctrl.version.$valid || $ctrl.version.$pristine">API Version (ex: 1.0)</div>
<div ng-messages="$ctrl.version.$error">
<div ng-message="required">API Version is required.</div>
<div ng-message="md-maxlength">The version has to be less than 32 characters long.</div>
</div>
</md-input-container>
</div>

<md-checkbox ng-repeat="fl in $ctrl.filteredFields" aria-label="fl.id" ng-model="fl.checked">
{{ fl.description }}
</md-checkbox>
</div>
</md-dialog-content>
<md-dialog-actions layout="row">
<md-button md-no-ink ng-click="$ctrl.hide()">
Cancel
</md-button>
<md-button type="submit" class="md-raised md-primary" ng-disabled="formApiDefinition.$invalid || $ctrl.contextPathInvalid">
Duplicate
</md-button>
</md-dialog-actions>
</form>
</md-dialog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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
*
* http://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.
*/
import * as _ from 'lodash';
import ApiService from "../../../../../services/api.service";

function DialogApiDuplicateController($mdDialog, ApiService: ApiService, api) {
'ngInject';
this.contextPathPlaceholder = api.proxy.virtual_hosts[0].path;
this.versionPlaceholder = api.version;
this.filteredFields = [
{id: "groups", description: "Groups", checked: true},
{id: "members", description: "Members", checked: true},
{id: "pages", description: "Pages", checked: true},
{id: "plans", description: "Plans", checked: true}
];

this.hide = () => {
$mdDialog.hide();
};

this.duplicate = () => {
const config = {
context_path: this.contextPath,
version: this.version,
filtered_fields: _.map(_.filter(this.filteredFields, (fl: any) => { return !fl.checked; }), "id")
};
ApiService.duplicate(api.id, config).then( (response) => {
$mdDialog.hide(response.data);
});
};

this.onContextPathChanged = () => {
this.contextPathInvalid = false;
ApiService.verify({context_path: this.contextPath}, {ignoreLoadingBar: true, silentCall: true}).then(() => {
this.contextPathInvalid = false;
}, () => {
this.contextPathInvalid = true;
});
};
}

export default DialogApiDuplicateController;
2 changes: 2 additions & 0 deletions src/management/management.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import LoginController from '../user/login/login.controller';
import DiffDirective from '../components/diff/diff.directive';
import DialogApiImportController from '../management/api/portal/general/dialog/apiImportDialog.controller';
import DialogApiExportController from '../management/api/portal/general/dialog/apiExportDialog.controller';
import DialogApiDuplicateController from '../management/api/portal/general/dialog/apiDuplicateDialog.controller';
// Sidenav
import SidenavService from '../components/sidenav/sidenav.service';
import {SidenavComponent} from '../components/sidenav/sidenav.component';
Expand Down Expand Up @@ -571,6 +572,7 @@ angular.module('gravitee-management', [uiRouter, permission, uiPermission, 'ngMa
.controller('UserController', UserController)
.controller('DialogApiImportController', DialogApiImportController)
.controller('DialogApiExportController', DialogApiExportController)
.controller('DialogApiDuplicateController', DialogApiDuplicateController)
.controller('DialogEditPolicyController', DialogEditPolicyController)
.controller('LoginController', LoginController)
.controller('InstancesController', InstancesController)
Expand Down
8 changes: 6 additions & 2 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,18 @@ class ApiService {
return this.$http.get(this.apisURL + apiId + '/export?exclude=' + exclude.join(",") + (exportVersion ? '&version=' + exportVersion : ''));
}

verify(criteria): ng.IPromise<any> {
return this.$http.post(this.apisURL + 'verify', criteria);
verify(criteria, config?): ng.IPromise<any> {
return this.$http.post(this.apisURL + 'verify', criteria, config);
}

importPathMappings(apiId, page): ng.IPromise<any> {
return this.$http.post(this.apisURL + apiId + '/import-path-mappings?page=' + page);
}

duplicate(apiId, config): ng.IPromise<any> {
return this.$http.post(this.apisURL + apiId + '/duplicate', config);
}

/*
* Analytics
*/
Expand Down

0 comments on commit d73fcc7

Please sign in to comment.