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

Avoid name clashing #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 27 additions & 1 deletion serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ const fixArrays = object => reduce(
{},
);

// follows https://github.com/serverless-components/aws-dynamodb
const setDomainName = (component, inputs, config) => {
const generatedName = inputs.name
? `${inputs.name}-${component.context.resourceId()}`
: component.context.resourceId();

const hasDeployedBefore = 'nameInput' in component.state;
const givenNameHasNotChanged = component.state.nameInput && component.state.nameInput === inputs.name;
const bothLastAndCurrentDeployHaveNoNameDefined = !component.state.nameInput && !inputs.name;

config.name = hasDeployedBefore && (givenNameHasNotChanged || bothLastAndCurrentDeployHaveNoNameDefined)
? component.state.name
: generatedName;

component.state.nameInput = inputs.name || false;
};

class AwsElasticsearch extends Component {
async default(inputs = {}) {
Expand All @@ -67,6 +83,10 @@ class AwsElasticsearch extends Component {
credentials: this.context.credentials.aws,
});

this.context.debug(`Checking if domain ${config.name} already exists in the ${config.region} region.`);

setDomainName(this, inputs, config);

let prevDomain = await getDomain({ elastic, ...config });

if (prevDomain && prevDomain.processing) {
Expand All @@ -76,14 +96,17 @@ class AwsElasticsearch extends Component {
}

if (!prevDomain) {
this.context.debug('Domain does not exist. A new one will be created.');
this.context.status('Creating');
const { arn, endpoint } = await createDomain({ elastic, ...config });
config.arn = arn;
config.endpoint = endpoint;
} else {
this.context.debug('Domain already exists..');
config.arn = prevDomain.arn;
config.endpoint = prevDomain.endpoint;
if (configChanged(prevDomain, config)) {
this.context.debug('Updating information.');
this.context.status('Updating');
await updateDomain({ elastic, ...config });
} else {
Expand All @@ -92,7 +115,10 @@ class AwsElasticsearch extends Component {
}

const outputs = pick(config, ['name', 'arn', 'endpoint', 'region']);
this.state = outputs;
this.state = {
...this.state,
...outputs,
};
await this.save();

// Return your outputs
Expand Down
12 changes: 9 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ const configChanged = (prevDomain, domain) => {
const keys = [
'name',
'accessPolicies',
'ebsOptions',
'elasticsearchClusterConfig',
'snapshotOptions',
];
const inputs = pick(domain, keys);
const prevInputs = pick(prevDomain, keys);

return !isMatch(prevInputs, inputs);
const ebsOptions = pick(domain.ebsOptions, ['EBSEnabled', 'VolumeSize', 'VolumeType']);
const prevEbsOptions = pick(prevDomain.ebsOptions, ['EBSEnabled', 'VolumeSize', 'VolumeType']);

const clusterConfig = pick(domain.elasticsearchClusterConfig, ['InstanceCount', 'InstanceType']);
const prevClusterConfig = pick(prevDomain.elasticsearchClusterConfig, ['InstanceCount', 'InstanceType']);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstn What is behind this?
are there any other "options" that we would need to compare under ebsOptions and elasticsearchClusterConfig ?


return !isMatch(prevInputs, inputs)
|| !isMatch(prevEbsOptions, ebsOptions)
|| !isMatch(prevClusterConfig, clusterConfig);
};

module.exports = {
Expand Down