Skip to content

Commit

Permalink
Add service catalog deprovision button (vmware-tanzu#60)
Browse files Browse the repository at this point in the history
* add deprovision

* delete selfLink instead of params

* remove delete url

* readability
  • Loading branch information
sozercan authored and prydonius committed Feb 13, 2018
1 parent e86ee13 commit 2bb39d2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/actions/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export function provision(
};
}

export function deprovision(instance: IServiceInstance) {
return async (dispatch: Dispatch<IStoreState>) => {
return ServiceCatalog.deprovisionInstance(instance);
};
}

export function sync(broker: IServiceBroker) {
return async (dispatch: Dispatch<IStoreState>) => {
return ServiceCatalog.syncBroker(broker);
Expand Down
7 changes: 6 additions & 1 deletion src/components/BrokerView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IServicePlan,
} from "../../shared/ServiceCatalog";
import { Card, CardContainer } from "../Card";
import DeprovisionButton from "../DeprovisionButton";
import SyncButton from "../SyncButton";

export interface IBrokerViewProps {
Expand All @@ -19,6 +20,7 @@ export interface IBrokerViewProps {
instances: IServiceInstance[];
plans: IServicePlan[];
sync: (broker: IServiceBroker) => Promise<any>;
deprovision: (instance: IServiceInstance) => Promise<any>;
}

export class BrokerView extends React.PureComponent<IBrokerViewProps> {
Expand All @@ -27,7 +29,7 @@ export class BrokerView extends React.PureComponent<IBrokerViewProps> {
}

public render() {
const { bindings, broker, instances } = this.props;
const { bindings, broker, instances, deprovision } = this.props;

return (
<div className="broker">
Expand Down Expand Up @@ -76,6 +78,9 @@ export class BrokerView extends React.PureComponent<IBrokerViewProps> {
<td key={message}>
<code>{message}</code>
</td>
<td>
<DeprovisionButton deprovision={deprovision} instance={instance} />
</td>
</tr>
);
})}
Expand Down
50 changes: 50 additions & 0 deletions src/components/DeprovisionButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as React from "react";
import { IServiceInstance } from "../../shared/ServiceCatalog";

interface IDeprovisionButtonProps {
instance: IServiceInstance;
deprovision: (instance: IServiceInstance) => Promise<{}>;
}

interface IDeprovisionButtonState {
error: string | undefined;
instance: IServiceInstance | undefined;
isDeprovisioning: boolean;
}

class DeprovisionButton extends React.Component<IDeprovisionButtonProps, IDeprovisionButtonState> {
public state: IDeprovisionButtonState = {
error: undefined,
instance: this.props.instance,
isDeprovisioning: false,
};

public handleDeprovision = async () => {
const { deprovision, instance } = this.props;
this.setState({ isDeprovisioning: true });

try {
await deprovision(instance);
this.setState({ isDeprovisioning: false });
} catch (err) {
this.setState({ isDeprovisioning: false, error: err.toString() });
}
};

public render() {
return (
<div className="DeprovisionButton">
{this.state.isDeprovisioning && <div>Deprovisioning...</div>}
<button
className="button button-primary"
disabled={this.state.isDeprovisioning}
onClick={this.handleDeprovision}
>
Deprovision
</button>
</div>
);
}
}

export default DeprovisionButton;
5 changes: 4 additions & 1 deletion src/containers/BrokerView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Dispatch } from "redux";

import actions from "../actions";
import { BrokerView } from "../components/BrokerView";
import { IServiceBroker } from "../shared/ServiceCatalog";
import { IServiceBroker, IServiceInstance } from "../shared/ServiceCatalog";
import { IStoreState } from "../shared/types";

interface IRouteProps {
Expand Down Expand Up @@ -43,6 +43,9 @@ function mapStateToProps({ catalog }: IStoreState, { match: { params } }: IRoute

function mapDispatchToProps(dispatch: Dispatch<IStoreState>) {
return {
deprovision: async (instance: IServiceInstance) => {
await dispatch(actions.catalog.deprovision(instance));
},
getCatalog: async () => {
dispatch(actions.catalog.getCatalog());
},
Expand Down
10 changes: 10 additions & 0 deletions src/shared/ServiceCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ export class ServiceCatalog {
return data;
}

public static async deprovisionInstance(instance: IServiceInstance) {
const { data } = await axios.delete("/api/kube" + instance.metadata.selfLink);

if (data.status === "Failure") {
throw new Error(data.message);
}

return data;
}

public static async syncBroker(broker: IServiceBroker) {
const { data } = await axios.patch<IStatus>(
urls.api.clusterservicebrokers.sync(broker),
Expand Down

0 comments on commit 2bb39d2

Please sign in to comment.