Skip to content

Commit

Permalink
feat(samples): add Service directory samples (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
katiehochberg authored and Aaron Gabriel Neyer committed Nov 17, 2022
1 parent 6122eda commit 7ef5874
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 9 deletions.
67 changes: 67 additions & 0 deletions service-directory/snippets/createEndpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
namespaceId = 'my-namespace',
serviceId = 'my-service',
endpointId = 'my-endpoint'
) {
// [START servicedirectory_create_endpoint]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';
// const endpointId = 'my-endpoint';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the service name
const serviceName = registrationServiceClient.servicePath(
projectId,
locationId,
namespaceId,
serviceId
);

async function createEndpoint() {
const [endpoint] = await registrationServiceClient.createEndpoint({
parent: serviceName,
endpointId: endpointId,
endpoint: {address: '10.0.0.1', port: 8080},
});

console.log(`Created endpoint: ${endpoint.name}`);
return endpoint;
}

return createEndpoint();
// [END servicedirectory_create_endpoint]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
60 changes: 60 additions & 0 deletions service-directory/snippets/createNamespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-central1',
namespaceId = 'my-namespace'
) {
// [START servicedirectory_create_namespace]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the location name
const locationName = registrationServiceClient.locationPath(
projectId,
locationId
);

async function createNamespace() {
const [namespace] = await registrationServiceClient.createNamespace({
parent: locationName,
namespaceId: namespaceId,
});

console.log(`Created namespace: ${namespace.name}`);
return namespace;
}

return createNamespace();
// [END servicedirectory_create_namespace]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
63 changes: 63 additions & 0 deletions service-directory/snippets/createService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
namespaceId = 'my-namespace',
serviceId = 'my-service'
) {
// [START servicedirectory_create_service]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the namespace name
const namespaceName = registrationServiceClient.namespacePath(
projectId,
locationId,
namespaceId
);

async function createService() {
const [service] = await registrationServiceClient.createService({
parent: namespaceName,
serviceId: serviceId,
});

console.log(`Created service: ${service.name}`);
return service;
}

return createService();
// [END servicedirectory_create_service]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
65 changes: 65 additions & 0 deletions service-directory/snippets/deleteEndpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
namespaceId = 'my-namespace',
serviceId = 'my-service',
endpointId = 'my-endpoint'
) {
// [START servicedirectory_delete_endpoint]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';
// const endpointId = 'my-endpoint';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the endpoint name
const endpointName = registrationServiceClient.endpointPath(
projectId,
locationId,
namespaceId,
serviceId,
endpointId
);

async function deleteEndpoint() {
await registrationServiceClient.deleteEndpoint({
name: endpointName,
});

console.log(`Deleted endpoint: ${endpointName}`);
}

deleteEndpoint();
// [END servicedirectory_delete_endpoint]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
59 changes: 59 additions & 0 deletions service-directory/snippets/deleteNamespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
namespaceId = 'my-namespace'
) {
// [START servicedirectory_delete_namespace]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the namespace name
const namespaceName = registrationServiceClient.namespacePath(
projectId,
locationId,
namespaceId
);

async function deleteNamespace() {
await registrationServiceClient.deleteNamespace({
name: namespaceName,
});

console.log(`Deleted namespace: ${namespaceName}`);
}

deleteNamespace();
// [END servicedirectory_delete_namespace]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
62 changes: 62 additions & 0 deletions service-directory/snippets/deleteService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

'use strict';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
namespaceId = 'my-namespace',
serviceId = 'my-service'
) {
// [START servicedirectory_delete_service]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';

// Imports the Google Cloud client library
const {
RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the service name
const serviceName = registrationServiceClient.servicePath(
projectId,
locationId,
namespaceId,
serviceId
);

async function deleteService() {
await registrationServiceClient.deleteService({
name: serviceName,
});

console.log(`Deleted service: ${serviceName}`);
}

deleteService();
// [END servicedirectory_delete_service]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
5 changes: 4 additions & 1 deletion service-directory/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"test": "c8 mocha --timeout 600000 test/*.js"
},
"dependencies": {
"@google-cloud/service-directory": "^1.0.1"
"@google-cloud/service-directory": "^1.0.1",
"eslint": "^7.0.0",
"json-schema": "^0.2.5",
"uuid": "^8.0.0"
},
"devDependencies": {
"c8": "^7.0.0",
Expand Down
Loading

0 comments on commit 7ef5874

Please sign in to comment.