Skip to content

Commit cbc8d5a

Browse files
katiehochbergAhrar Monsur
authored and
Ahrar Monsur
committed
feat(samples): add Service directory samples (#42)
1 parent 9f6de26 commit cbc8d5a

11 files changed

+595
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-east1',
21+
namespaceId = 'my-namespace',
22+
serviceId = 'my-service',
23+
endpointId = 'my-endpoint'
24+
) {
25+
// [START servicedirectory_create_endpoint]
26+
//
27+
// TODO(developer): Uncomment these variables before running the sample.
28+
//
29+
// const projectId = 'my-project';
30+
// const locationId = 'us-central1';
31+
// const namespaceId = 'my-namespace';
32+
// const serviceId = 'my-service';
33+
// const endpointId = 'my-endpoint';
34+
35+
// Imports the Google Cloud client library
36+
const {
37+
RegistrationServiceClient,
38+
} = require('@google-cloud/service-directory');
39+
40+
// Creates a client
41+
const registrationServiceClient = new RegistrationServiceClient();
42+
43+
// Build the service name
44+
const serviceName = registrationServiceClient.servicePath(
45+
projectId,
46+
locationId,
47+
namespaceId,
48+
serviceId
49+
);
50+
51+
async function createEndpoint() {
52+
const [endpoint] = await registrationServiceClient.createEndpoint({
53+
parent: serviceName,
54+
endpointId: endpointId,
55+
endpoint: {address: '10.0.0.1', port: 8080},
56+
});
57+
58+
console.log(`Created endpoint: ${endpoint.name}`);
59+
return endpoint;
60+
}
61+
62+
return createEndpoint();
63+
// [END servicedirectory_create_endpoint]
64+
}
65+
66+
const args = process.argv.slice(2);
67+
main(...args).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-central1',
21+
namespaceId = 'my-namespace'
22+
) {
23+
// [START servicedirectory_create_namespace]
24+
//
25+
// TODO(developer): Uncomment these variables before running the sample.
26+
//
27+
// const projectId = 'my-project';
28+
// const locationId = 'us-central1';
29+
// const namespaceId = 'my-namespace';
30+
31+
// Imports the Google Cloud client library
32+
const {
33+
RegistrationServiceClient,
34+
} = require('@google-cloud/service-directory');
35+
36+
// Creates a client
37+
const registrationServiceClient = new RegistrationServiceClient();
38+
39+
// Build the location name
40+
const locationName = registrationServiceClient.locationPath(
41+
projectId,
42+
locationId
43+
);
44+
45+
async function createNamespace() {
46+
const [namespace] = await registrationServiceClient.createNamespace({
47+
parent: locationName,
48+
namespaceId: namespaceId,
49+
});
50+
51+
console.log(`Created namespace: ${namespace.name}`);
52+
return namespace;
53+
}
54+
55+
return createNamespace();
56+
// [END servicedirectory_create_namespace]
57+
}
58+
59+
const args = process.argv.slice(2);
60+
main(...args).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-east1',
21+
namespaceId = 'my-namespace',
22+
serviceId = 'my-service'
23+
) {
24+
// [START servicedirectory_create_service]
25+
//
26+
// TODO(developer): Uncomment these variables before running the sample.
27+
//
28+
// const projectId = 'my-project';
29+
// const locationId = 'us-central1';
30+
// const namespaceId = 'my-namespace';
31+
// const serviceId = 'my-service';
32+
33+
// Imports the Google Cloud client library
34+
const {
35+
RegistrationServiceClient,
36+
} = require('@google-cloud/service-directory');
37+
38+
// Creates a client
39+
const registrationServiceClient = new RegistrationServiceClient();
40+
41+
// Build the namespace name
42+
const namespaceName = registrationServiceClient.namespacePath(
43+
projectId,
44+
locationId,
45+
namespaceId
46+
);
47+
48+
async function createService() {
49+
const [service] = await registrationServiceClient.createService({
50+
parent: namespaceName,
51+
serviceId: serviceId,
52+
});
53+
54+
console.log(`Created service: ${service.name}`);
55+
return service;
56+
}
57+
58+
return createService();
59+
// [END servicedirectory_create_service]
60+
}
61+
62+
const args = process.argv.slice(2);
63+
main(...args).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-east1',
21+
namespaceId = 'my-namespace',
22+
serviceId = 'my-service',
23+
endpointId = 'my-endpoint'
24+
) {
25+
// [START servicedirectory_delete_endpoint]
26+
//
27+
// TODO(developer): Uncomment these variables before running the sample.
28+
//
29+
// const projectId = 'my-project';
30+
// const locationId = 'us-central1';
31+
// const namespaceId = 'my-namespace';
32+
// const serviceId = 'my-service';
33+
// const endpointId = 'my-endpoint';
34+
35+
// Imports the Google Cloud client library
36+
const {
37+
RegistrationServiceClient,
38+
} = require('@google-cloud/service-directory');
39+
40+
// Creates a client
41+
const registrationServiceClient = new RegistrationServiceClient();
42+
43+
// Build the endpoint name
44+
const endpointName = registrationServiceClient.endpointPath(
45+
projectId,
46+
locationId,
47+
namespaceId,
48+
serviceId,
49+
endpointId
50+
);
51+
52+
async function deleteEndpoint() {
53+
await registrationServiceClient.deleteEndpoint({
54+
name: endpointName,
55+
});
56+
57+
console.log(`Deleted endpoint: ${endpointName}`);
58+
}
59+
60+
deleteEndpoint();
61+
// [END servicedirectory_delete_endpoint]
62+
}
63+
64+
const args = process.argv.slice(2);
65+
main(...args).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-east1',
21+
namespaceId = 'my-namespace'
22+
) {
23+
// [START servicedirectory_delete_namespace]
24+
//
25+
// TODO(developer): Uncomment these variables before running the sample.
26+
//
27+
// const projectId = 'my-project';
28+
// const locationId = 'us-central1';
29+
// const namespaceId = 'my-namespace';
30+
31+
// Imports the Google Cloud client library
32+
const {
33+
RegistrationServiceClient,
34+
} = require('@google-cloud/service-directory');
35+
36+
// Creates a client
37+
const registrationServiceClient = new RegistrationServiceClient();
38+
39+
// Build the namespace name
40+
const namespaceName = registrationServiceClient.namespacePath(
41+
projectId,
42+
locationId,
43+
namespaceId
44+
);
45+
46+
async function deleteNamespace() {
47+
await registrationServiceClient.deleteNamespace({
48+
name: namespaceName,
49+
});
50+
51+
console.log(`Deleted namespace: ${namespaceName}`);
52+
}
53+
54+
deleteNamespace();
55+
// [END servicedirectory_delete_namespace]
56+
}
57+
58+
const args = process.argv.slice(2);
59+
main(...args).catch(console.error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
'use strict';
17+
18+
async function main(
19+
projectId = 'my-project',
20+
locationId = 'us-east1',
21+
namespaceId = 'my-namespace',
22+
serviceId = 'my-service'
23+
) {
24+
// [START servicedirectory_delete_service]
25+
//
26+
// TODO(developer): Uncomment these variables before running the sample.
27+
//
28+
// const projectId = 'my-project';
29+
// const locationId = 'us-central1';
30+
// const namespaceId = 'my-namespace';
31+
// const serviceId = 'my-service';
32+
33+
// Imports the Google Cloud client library
34+
const {
35+
RegistrationServiceClient,
36+
} = require('@google-cloud/service-directory');
37+
38+
// Creates a client
39+
const registrationServiceClient = new RegistrationServiceClient();
40+
41+
// Build the service name
42+
const serviceName = registrationServiceClient.servicePath(
43+
projectId,
44+
locationId,
45+
namespaceId,
46+
serviceId
47+
);
48+
49+
async function deleteService() {
50+
await registrationServiceClient.deleteService({
51+
name: serviceName,
52+
});
53+
54+
console.log(`Deleted service: ${serviceName}`);
55+
}
56+
57+
deleteService();
58+
// [END servicedirectory_delete_service]
59+
}
60+
61+
const args = process.argv.slice(2);
62+
main(...args).catch(console.error);

service-directory/snippets/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"test": "c8 mocha --timeout 600000 test/*.js"
1414
},
1515
"dependencies": {
16-
"@google-cloud/service-directory": "^1.0.1"
16+
"@google-cloud/service-directory": "^1.0.1",
17+
"eslint": "^7.0.0",
18+
"json-schema": "^0.2.5",
19+
"uuid": "^8.0.0"
1720
},
1821
"devDependencies": {
1922
"c8": "^7.0.0",

0 commit comments

Comments
 (0)