From 8f824d47c5c6c5e15c1945772432ca8d5bb0acd7 Mon Sep 17 00:00:00 2001
From: Shinebayar G <3091558+shinebayar-g@users.noreply.github.com>
Date: Fri, 3 Nov 2023 03:29:49 -0700
Subject: [PATCH] feat(chart): implement `apiObjects` function (#1795)
Fixes #
This PR implements `get apiObjects(): ApiObject[]` function at the chart level. So that users can easily access all the k8s objects of the chart. Borrowed from [Include class.](https://github.com/cdk8s-team/cdk8s-core/blob/8da5c95703f438f375fe003d33966a06c45ce7b7/src/include.ts#L37-L42)
---
docs/java.md | 12 ++++++++++++
docs/python.md | 12 ++++++++++++
docs/typescript.md | 12 ++++++++++++
src/chart.ts | 8 +++++++-
src/include.ts | 2 +-
test/chart.test.ts | 27 +++++++++++++++++++++++++++
6 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/docs/java.md b/docs/java.md
index e825b2e44b..7092e74f87 100644
--- a/docs/java.md
+++ b/docs/java.md
@@ -512,6 +512,18 @@ a construct node.
#### Properties
+##### `apiObjects`Required
+
+```java
+public java.util.List getApiObjects();
+```
+
+- *Type:* java.util.List<[`org.cdk8s.ApiObject`](#org.cdk8s.ApiObject)>
+
+Returns all the included API objects.
+
+---
+
##### `labels`Required
```java
diff --git a/docs/python.md b/docs/python.md
index 52ee38cc61..8ccab9732d 100644
--- a/docs/python.md
+++ b/docs/python.md
@@ -534,6 +534,18 @@ a construct node.
#### Properties
+##### `api_objects`Required
+
+```python
+api_objects: typing.List[ApiObject]
+```
+
+- *Type:* typing.List[[`cdk8s.ApiObject`](#cdk8s.ApiObject)]
+
+Returns all the included API objects.
+
+---
+
##### `labels`Required
```python
diff --git a/docs/typescript.md b/docs/typescript.md
index 5ee390183f..c4da089e49 100644
--- a/docs/typescript.md
+++ b/docs/typescript.md
@@ -402,6 +402,18 @@ a construct node.
#### Properties
+##### `apiObjects`Required
+
+```typescript
+public readonly apiObjects: ApiObject[];
+```
+
+- *Type:* [`cdk8s.ApiObject`](#cdk8s.ApiObject)[]
+
+Returns all the included API objects.
+
+---
+
##### `labels`Required
```typescript
diff --git a/src/chart.ts b/src/chart.ts
index d81855ae9a..df23434e0f 100644
--- a/src/chart.ts
+++ b/src/chart.ts
@@ -147,5 +147,11 @@ export class Chart extends Construct {
public toJson(): any[] {
return App._synthChart(this);
}
-}
+ /**
+ * Returns all the included API objects.
+ */
+ get apiObjects(): ApiObject[] {
+ return this.node.children.filter((o): o is ApiObject => o instanceof ApiObject);
+ }
+}
diff --git a/src/include.ts b/src/include.ts
index 591a92ff9f..0f688d5906 100644
--- a/src/include.ts
+++ b/src/include.ts
@@ -38,6 +38,6 @@ export class Include extends Construct {
* Returns all the included API objects.
*/
public get apiObjects(): ApiObject[] {
- return this.node.children.filter(o => o instanceof ApiObject) as ApiObject[];
+ return this.node.children.filter((o): o is ApiObject => o instanceof ApiObject);
}
}
diff --git a/test/chart.test.ts b/test/chart.test.ts
index c1a4234b56..3a669a4101 100644
--- a/test/chart.test.ts
+++ b/test/chart.test.ts
@@ -450,3 +450,30 @@ class CustomNestedConstruct extends Construct {
this.obj = new CustomConstruct(this, 'nested');
}
}
+
+test('apiObjects returns all the API objects', () => {
+ // GIVEN
+ const chart = Testing.chart();
+
+ // WHEN
+
+ new ApiObject(chart, 'obj1', {
+ kind: 'Deployment',
+ apiVersion: 'v1',
+ });
+ new ApiObject(chart, 'obj2', {
+ apiVersion: 'v1',
+ kind: 'Foo',
+ metadata: {
+ name: 'resource1',
+ },
+ });
+ new ApiObject(chart, 'obj3', {
+ apiVersion: 'v1',
+ kind: 'Bar',
+ metadata: {
+ name: 'resource1',
+ },
+ });
+ expect(chart.apiObjects.map((x) => x.kind).sort()).toEqual(['Bar', 'Deployment', 'Foo']);
+});