2022-07-04 10:31:58 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2022-10-06 08:30:30 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2022-07-04 10:31:58 +00:00
|
|
|
|
2022-10-11 12:36:21 +00:00
|
|
|
function normalizePeerPayload(peerPayload, dc, partition) {
|
|
|
|
const {
|
|
|
|
StreamStatus: { LastHeartbeat, LastReceive, LastSend, ImportedServices, ExportedServices },
|
|
|
|
} = peerPayload;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...peerPayload,
|
|
|
|
LastHeartbeat,
|
|
|
|
LastReceive,
|
|
|
|
LastSend,
|
|
|
|
ImportedServices,
|
|
|
|
ExportedServices,
|
|
|
|
Datacenter: dc,
|
|
|
|
Partition: partition,
|
|
|
|
};
|
|
|
|
}
|
2022-07-04 10:31:58 +00:00
|
|
|
export default class PeerService extends RepositoryService {
|
2022-10-06 08:30:30 +00:00
|
|
|
@service store;
|
|
|
|
|
2022-07-04 10:31:58 +00:00
|
|
|
getModelName() {
|
|
|
|
return 'peer';
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:30:30 +00:00
|
|
|
@dataSource('/:partition/:ns/:ds/exported-services/:name')
|
|
|
|
async fetchExportedServices({ dc, ns, partition, name }, configuration, request) {
|
|
|
|
return (
|
|
|
|
await request`
|
|
|
|
GET /v1/internal/ui/exported-services
|
|
|
|
|
|
|
|
${{
|
|
|
|
peer: name,
|
|
|
|
}}
|
|
|
|
`
|
|
|
|
)((headers, body, cache) => {
|
|
|
|
const serviceSerializer = this.store.serializerFor('service');
|
|
|
|
|
|
|
|
return this.store.push(
|
|
|
|
serviceSerializer.createJSONApiDocumentFromServicesPayload(headers, body, dc)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-30 18:42:36 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/peering/token-for/:name/:externalAddresses')
|
|
|
|
async fetchToken({ dc, ns, partition, name, externalAddresses }, configuration, request) {
|
|
|
|
const ServerExternalAddresses =
|
|
|
|
externalAddresses?.length > 0 ? externalAddresses.split(',') : [];
|
|
|
|
|
2022-08-03 13:04:19 +00:00
|
|
|
return (
|
|
|
|
await request`
|
2022-07-14 10:23:16 +00:00
|
|
|
POST /v1/peering/token
|
|
|
|
|
|
|
|
${{
|
|
|
|
PeerName: name,
|
|
|
|
Partition: partition || undefined,
|
2022-11-30 18:42:36 +00:00
|
|
|
ServerExternalAddresses,
|
2022-07-14 10:23:16 +00:00
|
|
|
}}
|
2022-08-03 13:04:19 +00:00
|
|
|
`
|
|
|
|
)((headers, body, cache) => body);
|
2022-07-14 10:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 10:31:58 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/peers')
|
|
|
|
async fetchAll({ dc, ns, partition }, { uri }, request) {
|
2022-08-03 13:04:19 +00:00
|
|
|
return (
|
|
|
|
await request`
|
2022-07-04 10:31:58 +00:00
|
|
|
GET /v1/peerings
|
|
|
|
|
|
|
|
${{
|
|
|
|
partition,
|
|
|
|
}}
|
2022-08-03 13:04:19 +00:00
|
|
|
`
|
|
|
|
)((headers, body, cache) => {
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
version: 2,
|
|
|
|
interval: 10000,
|
|
|
|
uri: uri,
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
body: body.map((item) => {
|
2022-08-03 13:04:19 +00:00
|
|
|
return cache(
|
2022-10-11 13:02:22 +00:00
|
|
|
normalizePeerPayload(item, dc, partition),
|
2022-09-15 08:43:17 +00:00
|
|
|
(uri) => uri`peer:///${partition}/${ns}/${dc}/peer/${item.Name}`
|
2022-08-03 13:04:19 +00:00
|
|
|
);
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
2022-07-04 10:31:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 10:23:02 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/peer-generate/')
|
|
|
|
@dataSource('/:partition/:ns/:dc/peer-initiate/')
|
2022-07-04 10:31:58 +00:00
|
|
|
@dataSource('/:partition/:ns/:dc/peer/:name')
|
2022-08-03 13:04:19 +00:00
|
|
|
async fetchOne({ partition, ns, dc, name }, { uri }, request) {
|
2022-08-09 10:08:07 +00:00
|
|
|
if (typeof name === 'undefined' || name === '') {
|
2022-08-08 10:23:02 +00:00
|
|
|
const item = this.create({
|
2022-07-04 10:31:58 +00:00
|
|
|
Datacenter: dc,
|
|
|
|
Namespace: '',
|
|
|
|
Partition: partition,
|
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
item.meta = { cacheControl: 'no-store' };
|
2022-08-08 10:23:02 +00:00
|
|
|
return item;
|
2022-07-04 10:31:58 +00:00
|
|
|
}
|
2022-08-03 13:04:19 +00:00
|
|
|
return (
|
|
|
|
await request`
|
2022-07-04 10:31:58 +00:00
|
|
|
GET /v1/peering/${name}
|
|
|
|
|
|
|
|
${{
|
|
|
|
partition,
|
|
|
|
}}
|
2022-08-03 13:04:19 +00:00
|
|
|
`
|
|
|
|
)((headers, body, cache) => {
|
2022-10-11 14:53:09 +00:00
|
|
|
// we can't easily use fragments as we are working around the serializer
|
|
|
|
// layer
|
2022-10-11 14:51:55 +00:00
|
|
|
const { StreamStatus } = body;
|
2022-10-11 14:53:09 +00:00
|
|
|
|
|
|
|
if (StreamStatus) {
|
|
|
|
if (StreamStatus.LastHeartbeat) {
|
|
|
|
StreamStatus.LastHeartbeat = new Date(StreamStatus.LastHeartbeat);
|
|
|
|
}
|
|
|
|
if (StreamStatus.LastReceive) {
|
|
|
|
StreamStatus.LastReceive = new Date(StreamStatus.LastReceive);
|
|
|
|
}
|
|
|
|
if (StreamStatus.LastSend) {
|
|
|
|
StreamStatus.LastSend = new Date(StreamStatus.LastSend);
|
|
|
|
}
|
2022-10-11 14:51:55 +00:00
|
|
|
}
|
2022-10-11 14:53:09 +00:00
|
|
|
|
2022-08-03 13:04:19 +00:00
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
version: 2,
|
|
|
|
interval: 10000,
|
|
|
|
uri: uri,
|
|
|
|
},
|
|
|
|
body: cache(
|
2022-10-11 13:02:22 +00:00
|
|
|
normalizePeerPayload(body, dc, partition),
|
2022-09-15 08:43:17 +00:00
|
|
|
(uri) => uri`peer:///${partition}/${ns}/${dc}/peer/${body.Name}`
|
2022-08-03 13:04:19 +00:00
|
|
|
),
|
|
|
|
};
|
2022-07-04 10:31:58 +00:00
|
|
|
});
|
|
|
|
}
|
2022-07-07 17:23:26 +00:00
|
|
|
|
2022-07-14 10:23:16 +00:00
|
|
|
async persist(item, request) {
|
|
|
|
// mark it as ESTABLISHING ourselves as the request is successful
|
|
|
|
// and we don't have blocking queries here to get immediate updates
|
2022-08-03 13:04:19 +00:00
|
|
|
return (
|
|
|
|
await request`
|
2022-07-14 10:23:16 +00:00
|
|
|
POST /v1/peering/establish
|
|
|
|
|
|
|
|
${{
|
|
|
|
PeerName: item.Name,
|
|
|
|
PeeringToken: item.PeeringToken,
|
|
|
|
Partition: item.Partition || undefined,
|
|
|
|
}}
|
2022-08-03 13:04:19 +00:00
|
|
|
`
|
|
|
|
)((headers, body, cache) => {
|
|
|
|
const partition = item.Partition;
|
|
|
|
const ns = item.Namespace;
|
|
|
|
const dc = item.Datacenter;
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
version: 2,
|
|
|
|
},
|
|
|
|
body: cache(
|
|
|
|
{
|
|
|
|
...item,
|
|
|
|
State: 'ESTABLISHING',
|
2022-07-14 10:23:16 +00:00
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
(uri) => uri`peer:///${partition}/${ns}/${dc}/peer/${item.Name}`
|
2022-08-03 13:04:19 +00:00
|
|
|
),
|
|
|
|
};
|
2022-07-14 10:23:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:23:26 +00:00
|
|
|
async remove(item, request) {
|
|
|
|
// soft delete
|
|
|
|
// we just return the item we want to delete
|
2022-07-14 10:23:16 +00:00
|
|
|
// but mark it as DELETING ourselves as the request is successful
|
2022-07-07 17:23:26 +00:00
|
|
|
// and we don't have blocking queries here to get immediate updates
|
2022-08-03 13:04:19 +00:00
|
|
|
return (
|
|
|
|
await request`
|
2022-07-07 17:23:26 +00:00
|
|
|
DELETE /v1/peering/${item.Name}
|
2022-08-03 13:04:19 +00:00
|
|
|
`
|
|
|
|
)((headers, body, cache) => {
|
|
|
|
const partition = item.Partition;
|
|
|
|
const ns = item.Namespace;
|
|
|
|
const dc = item.Datacenter;
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
version: 2,
|
|
|
|
},
|
|
|
|
body: cache(
|
|
|
|
{
|
|
|
|
...item,
|
|
|
|
State: 'DELETING',
|
2022-07-07 17:23:26 +00:00
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
(uri) => uri`peer:///${partition}/${ns}/${dc}/peer/${item.Name}`
|
2022-08-03 13:04:19 +00:00
|
|
|
),
|
|
|
|
};
|
2022-07-07 17:23:26 +00:00
|
|
|
});
|
|
|
|
}
|
2022-07-04 10:31:58 +00:00
|
|
|
}
|