@adpt/cloud
Version:
AdaptJS cloud component library
110 lines • 4.68 kB
JavaScript
"use strict";
/*
* Copyright 2018-2019 Unbounded Systems, 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
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = tslib_1.__importStar(require("@adpt/core"));
const utils_1 = require("@adpt/utils");
const lodash_1 = require("lodash");
const CFResource_1 = require("./CFResource");
const credentials_1 = require("./credentials");
const ec2_observer_1 = require("./ec2_observer");
const plugin_utils_1 = require("./plugin_utils");
const resourceType = "AWS::EC2::Instance";
/** @beta */
class EC2InstanceNC extends core_1.Component {
build() {
const props = this.props;
const properties = utils_1.removeUndef({
InstanceType: props.instanceType,
KeyName: props.sshKeyName,
ImageId: props.imageId,
SecurityGroups: props.securityGroups,
Tags: props.name ? [{ Key: "Name", Value: props.name }] : undefined,
UserData: (typeof props.userData === "string") ?
Buffer.from(props.userData).toString("base64") : undefined,
});
return (core_1.default.createElement(CFResource_1.CFResource, { key: props.key, Type: resourceType, Properties: properties }, props.children));
}
async ready(helpers) {
const hand = this.props.handle;
if (!hand)
return false;
const status = await helpers.elementStatus(hand);
if (!status)
return false;
return status.State != null && status.State.Name === "running";
}
async status(observe, buildData) {
const isActive = (inst) => inst.State && inst.State.Name !== "terminated";
const { awsCredentials } = this.props;
const hand = this.props.handle;
if (!hand)
throw new Error(`EC2InstanceNC component handle is null`);
const resourceId = plugin_utils_1.adaptResourceId(hand);
if (awsCredentials == null) {
throw new Error(`awsCredentials must be provided`);
}
const obsP = observe(ec2_observer_1.AwsEc2Observer, core_1.gql `
query (
$input: DescribeInstancesRequest_input!,
$awsAccessKeyId: String!,
$awsSecretAccessKey: String!,
$awsRegion: String!
) {
withCredentials(
awsAccessKeyId: $awsAccessKeyId,
awsSecretAccessKey: $awsSecretAccessKey,
awsRegion: $awsRegion
) {
DescribeInstances(body: $input) @all(depth: 10)
}
}`, Object.assign({ input: {
Filters: [
{
Name: `tag:${plugin_utils_1.adaptResourceIdTag}`,
Values: [resourceId]
},
{
Name: `tag:${plugin_utils_1.adaptDeployIdTag}`,
Values: [buildData.deployID]
}
]
} }, awsCredentials));
return core_1.mergeDefaultChildStatus(this.props, obsP, observe, buildData, (obs) => {
let noStatus;
const reservations = obs.withCredentials.DescribeInstances.Reservations;
if (!Array.isArray(reservations)) {
noStatus = `Unexpected response from AWS API: ${reservations}`;
}
else {
const instances = lodash_1.flatten(reservations.map((r) => r.Instances));
const active = instances.filter(isActive);
if (active.length === 1)
return active[0];
noStatus = active.length === 0 ?
`EC2Instance with ID ${resourceId} does not exist` :
`Multiple EC2Instances with ID ${resourceId} exist`;
}
const stat = { noStatus };
return stat;
});
}
}
/** @beta */
// tslint:disable-next-line:variable-name
exports.EC2Instance = credentials_1.withCredentials(EC2InstanceNC);
//# sourceMappingURL=EC2Instance.js.map