Skip to content

Commit dca712e

Browse files
authored
Merge pull request #250 from platformsh/feature/enviroment-domains
added creation, deletion and get custom or environment domains
2 parents e28a93e + d254eeb commit dca712e

File tree

4 files changed

+151
-3
lines changed

4 files changed

+151
-3
lines changed

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ export default class Client {
316316
return entities.EnvironmentBackup.query({ projectId, environmentId });
317317
}
318318

319+
/**
320+
* Get domains of environment {environmentId} of project {projectId}
321+
*
322+
* @param projectId
323+
* @param environmentId
324+
*
325+
* @return Promise EnvironmentDomains[]
326+
*/
327+
async getEnvironmentDomains(projectId: string, environmentId: string) {
328+
return entities.EnvironmentDomain.query({ projectId, environmentId });
329+
}
330+
319331
/**
320332
* Get the integrations of project projectId
321333
*

src/model/EnvironmentDomain.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { getConfig } from "../config";
2+
3+
import type { APIObject } from "./Ressource";
4+
import Ressource from "./Ressource";
5+
6+
const paramDefaults = {};
7+
const modifiableField = ["is_default"];
8+
const _url = "/projects/:projectId/environments/:environmentId/domains";
9+
10+
export type EnvironmentDomainGetParams = {
11+
[index: string]: any;
12+
name: string;
13+
projectId?: string;
14+
environmentId?: string;
15+
};
16+
17+
export type EnvironmentDomainQueryParams = {
18+
[index: string]: any;
19+
projectId: string;
20+
environmentId: string;
21+
};
22+
23+
export default class EnvironmentDomain extends Ressource {
24+
id = "";
25+
name = "";
26+
is_default = false;
27+
created_at = "";
28+
ssl = [];
29+
updated_at = "";
30+
replacement_for = "";
31+
type = "";
32+
33+
constructor(environmentDomain: APIObject, url: string) {
34+
super(
35+
url,
36+
paramDefaults,
37+
{},
38+
environmentDomain,
39+
["name", "ssl", "is_default", "type", "replacement_for"],
40+
modifiableField
41+
);
42+
this._required = ["name"];
43+
}
44+
45+
static async get(
46+
params: EnvironmentDomainGetParams,
47+
customUrl?: string
48+
): Promise<EnvironmentDomain> {
49+
const { name, projectId, environmentId, ...queryParams } = params;
50+
const { api_url } = getConfig();
51+
52+
return super._get<EnvironmentDomain>(
53+
customUrl ? `${customUrl}/:name` : `${api_url}${_url}`,
54+
{ name, projectId, environmentId },
55+
paramDefaults,
56+
queryParams
57+
);
58+
}
59+
60+
static async query(
61+
params: EnvironmentDomainQueryParams,
62+
customUrl?: string
63+
): Promise<EnvironmentDomain[]> {
64+
const { projectId, environmentId, ...queryParams } = params;
65+
const { api_url } = getConfig();
66+
67+
return super._query(
68+
customUrl ?? `${api_url}${_url}`,
69+
{ projectId, environmentId },
70+
paramDefaults,
71+
queryParams
72+
);
73+
}
74+
}

src/model/Project.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import "cross-fetch/polyfill"; // fetch api polyfill
2-
import parse_url from "parse_url";
32
import path from "path";
43

4+
import parse_url from "parse_url";
5+
56
import request, { createEventSource } from "../api";
67
import { getConfig } from "../config";
78

89
import Activity from "./Activity";
910
import Certificate from "./Certificate";
1011
import Domain from "./Domain";
1112
import Environment from "./Environment";
13+
import EnvironmentDomain from "./EnvironmentDomain";
1214
import Integration from "./Integration";
1315
import ProjectAccess from "./ProjectAccess";
1416
import ProjectLevelVariable from "./ProjectLevelVariable";
@@ -50,6 +52,7 @@ export default class Project extends Ressource {
5052
plan_uri = "";
5153
subscription: Subscription = new Subscription({});
5254
subscription_id = "";
55+
environment_id = "";
5356
status = "";
5457
endpoint = "";
5558
repository: Repository = {
@@ -211,6 +214,63 @@ export default class Project extends Ressource {
211214
return Domain.query({ projectId: this.id, limit }, this.getLink("domains"));
212215
}
213216

217+
/**
218+
* Get a list of environment domains for the project.
219+
*
220+
* @param int limit
221+
*
222+
* @return EnvironmentDomain[]
223+
*/
224+
async getEnvironmentDomains(environmentId: string) {
225+
return EnvironmentDomain.query(
226+
{ projectId: this.id, environmentId },
227+
this.getLink(`environments/${environmentId}/domains`)
228+
);
229+
}
230+
231+
/**
232+
* Add a environmentDomain to the environment project.
233+
*
234+
* @param string name
235+
* @param string environmentId
236+
* @param string type
237+
* @param string replacement_for
238+
* @param array ssl
239+
*
240+
* @return Result
241+
*/
242+
async addEnvironmentDomain(
243+
name: string,
244+
environmentId: string,
245+
type = "replacement",
246+
replacement_for?: string,
247+
ssl?: never[]
248+
) {
249+
const body: Partial<EnvironmentDomain> = { name, type, replacement_for };
250+
251+
if (ssl) {
252+
body.ssl = ssl;
253+
}
254+
255+
if (replacement_for) {
256+
body.replacement_for = replacement_for;
257+
} else {
258+
body.is_default = true;
259+
}
260+
261+
if (!replacement_for) {
262+
body.is_default = true;
263+
}
264+
body.type = type;
265+
266+
const environmentDomain = new EnvironmentDomain(
267+
body,
268+
this.getLink(`environments/${environmentId}/domains`)
269+
);
270+
271+
return environmentDomain.save();
272+
}
273+
214274
/**
215275
* Get a single domain of the project.
216276
*
@@ -307,8 +367,8 @@ export default class Project extends Ressource {
307367
*
308368
* @return Activity[]
309369
*/
310-
getActivities(types: Array<string>, starts_at?: Date) {
311-
const startsAt = starts_at?.toISOString()
370+
async getActivities(types: string[], starts_at?: Date) {
371+
const startsAt = starts_at?.toISOString();
312372
const params = { type: types, starts_at: startsAt };
313373

314374
return Activity.query(params, `${this.getUri()}/activities`);

src/model/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Domain from "./Domain";
1212
import Environment from "./Environment";
1313
import EnvironmentAccess from "./EnvironmentAccess";
1414
import EnvironmentBackup from "./EnvironmentBackup";
15+
import EnvironmentDomain from "./EnvironmentDomain";
1516
import EnvironmentType from "./EnvironmentType";
1617
import Blob from "./git/Blob";
1718
import Commit from "./git/Commit";
@@ -64,6 +65,7 @@ export default {
6465
Domain,
6566
EnvironmentAccess,
6667
EnvironmentBackup,
68+
EnvironmentDomain,
6769
Metrics,
6870
ProjectAccess,
6971
ProjectLevelVariable,

0 commit comments

Comments
 (0)