Cc 7146/convert consul hcp to a simpler component for some upcoming changes (#20344)

* Convert consul-hcp to a simpler component

* update existing test to use envStub helper

* Add missing copyright - weird no one yelled about this

* remove consul-hcp from load

* remove this test that doesn't run anymore
This commit is contained in:
Chris Hut 2024-01-26 16:39:50 -08:00 committed by GitHub
parent 1c1df4530c
commit 75af5b15d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 131 additions and 161 deletions

View File

@ -11,7 +11,7 @@
"scripts": {
"doc:toc": "doctoc README.md",
"compliance": "npm-run-all compliance:*",
"compliance:licenses": "license-checker --summary --onlyAllow 'Python-2.0;Apache*;Apache License, Version 2.0;Apache-2.0;Apache 2.0;Artistic-2.0;BSD;BSD-3-Clause;CC-BY-3.0;CC-BY-4.0;CC0-1.0;ISC;MIT;MPL-2.0;Public Domain;Unicode-TOU;Unlicense;WTFPL' --excludePackages 'consul-ui@2.2.0;consul-acls@0.1.0;consul-lock-sessions@0.1.0;consul-partitions@0.1.0;consul-nspaces@0.1.0;consul-hcp@0.1.0;consul-peerings@0.1.0'"
"compliance:licenses": "license-checker --summary --onlyAllow 'Python-2.0;Apache*;Apache License, Version 2.0;Apache-2.0;Apache 2.0;Artistic-2.0;BSD;BSD-3-Clause;CC-BY-3.0;CC-BY-4.0;CC0-1.0;ISC;MIT;MPL-2.0;Public Domain;Unicode-TOU;Unlicense;WTFPL' --excludePackages 'consul-ui@2.2.0;consul-acls@0.1.0;consul-lock-sessions@0.1.0;consul-partitions@0.1.0;consul-nspaces@0.1.0;consul-peerings@0.1.0'"
},
"devDependencies": {

View File

@ -1,15 +0,0 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: BUSL-1.1
}}
{{#let @list (env "CONSUL_HCP_URL") as |SNL hcpUrl|}}
{{#if (and SNL hcpUrl)}}
<SNL.BackLink
@text={{t "components.hashicorp-consul.side-nav.hcp"}}
@href={{hcpUrl}}
@isHrefExternal={{true}}
data-test-back-to-hcp
/>
{{/if}}
{{/let}}

View File

@ -1,80 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import ConsulHcpHome from 'consul-ui/components/consul/hcp/home';
module('Integration | Component | consul hcp home', function(hooks) {
setupRenderingTest(hooks);
test('it prints the value of CONSUL_HCP_URL', async function(assert) {
// temporary registration until we are running as separate applications
this.owner.register(
'component:consul/hcp/home',
ConsulHcpHome
);
//
const Helper = this.owner.resolveRegistration('helper:env');
this.owner.register(
'helper:env',
class extends Helper {
compute([name, def]) {
switch(name) {
case 'CONSUL_HCP_URL':
return 'http://hcp';
}
return super.compute(...arguments);
}
}
);
await render(hbs`
<Hds::SideNav::List as |SNL|>
<Consul::Hcp::Home @list={{SNL}} />
</Hds::SideNav::List>
`);
assert.dom('[data-test-back-to-hcp]').isVisible();
assert.dom('a').hasAttribute('href', 'http://hcp');
});
test('it does not output the Back to HCP link if CONSUL_HCP_URL is not present', async function(assert) {
// temporary registration until we are running as separate applications
this.owner.register(
'component:consul/hcp/home',
ConsulHcpHome
);
//
const Helper = this.owner.resolveRegistration('helper:env');
this.owner.register(
'helper:env',
class extends Helper {
compute([name, def]) {
switch(name) {
case 'CONSUL_HCP_URL':
return undefined;
}
return super.compute(...arguments);
}
}
);
await render(hbs`
<Hds::SideNav::List as |SNL|>
<Consul::Hcp::Home @list={{SNL}} />
</Hds::SideNav::List>
`);
assert.dom('[data-test-back-to-hcp]').doesNotExist();
assert.dom('a').doesNotExist();
});
});

View File

@ -1,5 +0,0 @@
{
"name": "consul-hcp",
"version": "0.1.0",
"private": true
}

View File

@ -1,14 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
(routes => routes({
dc: {
show: null
},
}))(
(json, data = (typeof document !== 'undefined' ? document.currentScript.dataset : module.exports)) => {
data[`routes`] = JSON.stringify(json);
}
);

View File

@ -1,14 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
(services => services({
'component:consul/hcp/home': {
class: 'consul-ui/components/consul/hcp/home',
},
}))(
(json, data = (typeof document !== 'undefined' ? document.currentScript.dataset : module.exports)) => {
data[`services`] = JSON.stringify(json);
}
);

View File

@ -122,7 +122,7 @@
class='hds-side-nav-hide-when-minimized consul-side-nav__selector-group'
as |SNL|
>
<Consul::Hcp::Home @list={{SNL}} />
<HcpNavItem @list={{SNL}}/>
<Consul::Datacenter::Selector
@list={{SNL}}
@dc={{@dc}}

View File

@ -0,0 +1,15 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: BUSL-1.1
}}
{{#let @list (env "CONSUL_HCP_URL") as |SNL hcpUrl|}}
{{#if this.shouldShowBackToHcpItem}}
<SNL.BackLink
@text={{t "components.hashicorp-consul.side-nav.hcp"}}
@href={{hcpUrl}}
@isHrefExternal={{true}}
data-test-back-to-hcp
/>
{{/if}}
{{/let}}

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
/**
* If the user has accessed consul from HCP managed consul, we do NOT want to display the
* "HCP Consul Central↗" link in the nav bar. As we're already displaying a BackLink to HCP.
*/
export default class HcpLinkItemComponent extends Component {
@service env;
get shouldShowBackToHcpItem() {
const isConsulHcpUrlDefined = !!this.env.var('CONSUL_HCP_URL');
const isConsulHcpEnabled = !!this.env.var('CONSUL_HCP_ENABLED');
return isConsulHcpEnabled && isConsulHcpUrlDefined;
}
}

View File

@ -39,7 +39,6 @@ module.exports = function (defaults, $ = process.env) {
'consul-peerings',
'consul-partitions',
'consul-nspaces',
'consul-hcp',
].map((item) => {
return {
name: item,

View File

@ -75,10 +75,6 @@ ${
{{if .NamespacesEnabled}}
<script src="${rootURL}assets/consul-nspaces/routes.js"></script>
{{end}}
{{if .HCPEnabled}}
<script src="${rootURL}assets/consul-hcp/services.js"></script>
<script src="${rootURL}assets/consul-hcp/routes.js"></script>
{{end}}
`
: `
<script>
@ -112,10 +108,6 @@ ${
name: 'consul-nspaces',
default: ${config.operatorConfig.NamespacesEnabled}
},
'CONSUL_HCP_ENABLE': {
name: 'consul-hcp',
default: ${config.operatorConfig.HCPEnabled}
},
}
);
</script>

View File

@ -91,7 +91,6 @@
"chalk": "^4.1.0",
"clipboard": "^2.0.11",
"consul-acls": "*",
"consul-hcp": "*",
"consul-lock-sessions": "*",
"consul-nspaces": "*",
"consul-partitions": "*",

View File

@ -1,7 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
// temporary import until we are running as separate applications
import 'consul-ui/components/consul/hcp/home/index.test';

View File

@ -0,0 +1,76 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { EnvStub } from 'consul-ui/services/env';
module('Integration | Component | hcp nav item', function (hooks) {
setupRenderingTest(hooks);
test('it prints the value of CONSUL_HCP_URL', async function (assert) {
this.owner.register(
'service:env',
class Stub extends EnvStub {
stubEnv = {
CONSUL_HCP_URL: 'http://hcp.com',
CONSUL_HCP_ENABLED: true,
};
}
);
await render(hbs`
<Hds::SideNav::List as |SNL|>
<HcpNavItem @list={{SNL}} />
</Hds::SideNav::List>
`);
assert.dom('[data-test-back-to-hcp]').isVisible();
assert.dom('a').hasAttribute('href', 'http://hcp.com');
});
test('it does not output the Back to HCP link if CONSUL_HCP_URL is not present', async function (assert) {
this.owner.register(
'service:env',
class Stub extends EnvStub {
stubEnv = {
CONSUL_HCP_ENABLED: true,
CONSUL_HCP_URL: undefined,
};
}
);
await render(hbs`
<Hds::SideNav::List as |SNL|>
<HcpNavItem @list={{SNL}} />
</Hds::SideNav::List>
`);
assert.dom('[data-test-back-to-hcp]').doesNotExist();
assert.dom('a').doesNotExist();
});
test('it does not output the Back to HCP link if CONSUL_HCP_ENABLED is not present', async function (assert) {
this.owner.register(
'service:env',
class Stub extends EnvStub {
stubEnv = {
CONSUL_HCP_URL: 'http://hcp.com',
CONSUL_HCP_ENABLED: undefined,
};
}
);
await render(hbs`
<Hds::SideNav::List as |SNL|>
<HcpNavItem @list={{SNL}} />
</Hds::SideNav::List>
`);
assert.dom('[data-test-back-to-hcp]').doesNotExist();
assert.dom('a').doesNotExist();
});
});

View File

@ -9,6 +9,7 @@ import { click, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Service from '@ember/service';
import sinon from 'sinon';
import { EnvStub } from 'consul-ui/services/env';
const userDismissedBannerStub = sinon.stub();
const dismissHcpLinkBannerStub = sinon.stub();
@ -24,16 +25,16 @@ module('Integration | Component | link-to-hcp-banner', function (hooks) {
dismissHcpLinkBanner = dismissHcpLinkBannerStub;
}
class EnvStub extends Service {
isEnterprise = false;
var(key) {
return key;
}
}
hooks.beforeEach(function () {
this.owner.register('service:hcp-link-status', HcpLinkStatusStub);
this.owner.register('service:env', EnvStub);
this.owner.register(
'service:env',
class Stub extends EnvStub {
stubEnv = {
isEnterprise: false,
};
}
);
});
test('it renders banner when hcp-link-status says it should', async function (assert) {
@ -96,14 +97,16 @@ module('Integration | Component | link-to-hcp-banner', function (hooks) {
});
test('it displays different banner text when consul is enterprise', async function (assert) {
class EnvStub extends Service {
isEnterprise = true;
var(key) {
return key;
this.owner.register(
'service:env',
class Stub extends EnvStub {
stubEnv = {
isEnterprise: true,
};
}
}
this.owner.register('service:env', EnvStub);
);
this.linkData = { isLinked: false };
await render(hbs`<LinkToHcpBanner @linkData={{this.linkData}} />`);
assert
.dom('[data-test-link-to-hcp-banner-description]')