Skip to content

Commit

Permalink
feat(chart): implement apiObjects function (#1795)
Browse files Browse the repository at this point in the history
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)
shinebayar-g authored Nov 3, 2023
1 parent f443176 commit 8f824d4
Showing 6 changed files with 71 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/java.md
Original file line number Diff line number Diff line change
@@ -512,6 +512,18 @@ a construct node.

#### Properties <a name="Properties"></a>

##### `apiObjects`<sup>Required</sup> <a name="org.cdk8s.Chart.property.apiObjects"></a>

```java
public java.util.List<ApiObject> getApiObjects();
```

- *Type:* java.util.List<[`org.cdk8s.ApiObject`](#org.cdk8s.ApiObject)>

Returns all the included API objects.

---

##### `labels`<sup>Required</sup> <a name="org.cdk8s.Chart.property.labels"></a>

```java
12 changes: 12 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
@@ -534,6 +534,18 @@ a construct node.

#### Properties <a name="Properties"></a>

##### `api_objects`<sup>Required</sup> <a name="cdk8s.Chart.property.api_objects"></a>

```python
api_objects: typing.List[ApiObject]
```

- *Type:* typing.List[[`cdk8s.ApiObject`](#cdk8s.ApiObject)]

Returns all the included API objects.

---

##### `labels`<sup>Required</sup> <a name="cdk8s.Chart.property.labels"></a>

```python
12 changes: 12 additions & 0 deletions docs/typescript.md
Original file line number Diff line number Diff line change
@@ -402,6 +402,18 @@ a construct node.

#### Properties <a name="Properties"></a>

##### `apiObjects`<sup>Required</sup> <a name="cdk8s.Chart.property.apiObjects"></a>

```typescript
public readonly apiObjects: ApiObject[];
```

- *Type:* [`cdk8s.ApiObject`](#cdk8s.ApiObject)[]

Returns all the included API objects.

---

##### `labels`<sup>Required</sup> <a name="cdk8s.Chart.property.labels"></a>

```typescript
8 changes: 7 additions & 1 deletion src/chart.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/include.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
27 changes: 27 additions & 0 deletions test/chart.test.ts
Original file line number Diff line number Diff line change
@@ -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']);
});

0 comments on commit 8f824d4

Please sign in to comment.