Skip to content

Commit 239a735

Browse files
authored
Merge pull request #1 from bigcommerce/circleci
Support new CircleCI stanza for Catalog Entities
2 parents bb0e143 + 459ec49 commit 239a735

File tree

7 files changed

+181
-3
lines changed

7 files changed

+181
-3
lines changed

docs/resources/catalog_entity.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Catalog Entity
2828
- `checkmarx` (Attributes) Checkmarx configuration for the entity. (see [below for nested schema](#nestedatt--checkmarx))
2929
- `children` (Attributes List) List of child entities for the entity. Only used for entities of type `TEAM` or `DOMAIN`. (see [below for nested schema](#nestedatt--children))
3030
- `ci_cd` (Attributes) CI/CD configuration for the entity. (see [below for nested schema](#nestedatt--ci_cd))
31+
- `circle_ci` (Attributes) CircleCI configuration for the entity. (see [below for nested schema](#nestedatt--circle_ci))
3132
- `coralogix` (Attributes) Coralogix configuration for the entity. (see [below for nested schema](#nestedatt--coralogix))
3233
- `dashboards` (Attributes) Dashboards configuration for the entity. (see [below for nested schema](#nestedatt--dashboards))
3334
- `definition` (String) Set when the entity is a Resource. These are the properties defined by the Resource Definition, in JSON format in a string (use the `jsonencode` function to convert a JSON object to a string).
@@ -176,6 +177,26 @@ Required:
176177

177178

178179

180+
<a id="nestedatt--circle_ci"></a>
181+
### Nested Schema for `circle_ci`
182+
183+
Optional:
184+
185+
- `projects` (Attributes List) List of CircleCI projects for the entity. (see [below for nested schema](#nestedatt--circle_ci--projects))
186+
187+
<a id="nestedatt--circle_ci--projects"></a>
188+
### Nested Schema for `circle_ci.projects`
189+
190+
Required:
191+
192+
- `slug` (String) The slug of the project in CircleCI.
193+
194+
Optional:
195+
196+
- `alias` (String) CircleCI alias. Only relevant if you've opted into multi-account support.
197+
198+
199+
179200
<a id="nestedatt--coralogix"></a>
180201
### Nested Schema for `coralogix`
181202

examples/resources/catalog_entity/resource.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ resource "cortex_catalog_entity" "products-service" {
263263
]
264264
}
265265

266+
circle_ci = {
267+
projects = [
268+
{ name = "products-service" }
269+
]
270+
}
271+
266272
coralogix = {
267273
applications = [
268274
{ name = "products-service" }

internal/cortex/catalog_entity_parser.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ func (c *CatalogEntityParser) YamlToEntity(yamlEntity map[string]interface{}) (C
109109
c.interpolateCheckmarx(&entity, info["x-cortex-checkmarx"].(map[string]interface{}))
110110
}
111111

112+
if info["x-cortex-circle-ci"] != nil {
113+
c.interpolateCircleCi(&entity, info["x-cortex-circle-ci"].(map[string]interface{}))
114+
}
115+
112116
if info["x-cortex-coralogix"] != nil {
113117
c.interpolateCoralogix(&entity, info["x-cortex-coralogix"].(map[string]interface{}))
114118
}
@@ -960,6 +964,24 @@ func (c *CatalogEntityParser) interpolateCiCdBuildkite(entity *CatalogEntityData
960964
}
961965
}
962966

967+
/***********************************************************************************************************************
968+
* CircleCi
969+
**********************************************************************************************************************/
970+
971+
func (c *CatalogEntityParser) interpolateCircleCi(entity *CatalogEntityData, cciMap map[string]interface{}) {
972+
entity.CircleCi = CatalogEntityCircleCi{}
973+
if cciMap["projects"] != nil {
974+
projects := cciMap["projects"].([]interface{})
975+
for _, project := range projects {
976+
projectMap := project.(map[string]interface{})
977+
entity.CircleCi.Projects = append(entity.CircleCi.Projects, CatalogEntityCircleCiProject{
978+
Slug: MapFetchToString(projectMap, "projectSlug"),
979+
Alias: MapFetchToString(projectMap, "alias"),
980+
})
981+
}
982+
}
983+
}
984+
963985
/***********************************************************************************************************************
964986
* Team attributes
965987
**********************************************************************************************************************/

internal/cortex/integrations.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type CatalogEntityData struct {
3636
// Integration-specific things
3737
BugSnag CatalogEntityBugSnag `json:"x-cortex-bugsnag,omitempty" yaml:"x-cortex-bugsnag,omitempty"`
3838
Checkmarx CatalogEntityCheckmarx `json:"x-cortex-checkmarx,omitempty" yaml:"x-cortex-checkmarx,omitempty"`
39+
CircleCi CatalogEntityCircleCi `json:"x-cortex-circle-ci,omitempty" yaml:"x-cortex-circle-ci,omitempty"`
3940
Coralogix CatalogEntityCoralogix `json:"x-cortex-coralogix,omitempty" yaml:"x-cortex-coralogix,omitempty"`
4041
FireHydrant CatalogEntityFireHydrant `json:"x-cortex-firehydrant,omitempty" yaml:"x-cortex-firehydrant,omitempty"`
4142
LaunchDarkly CatalogEntityLaunchDarkly `json:"x-cortex-launch-darkly,omitempty" yaml:"x-cortex-launch-darkly,omitempty"`
@@ -313,7 +314,7 @@ func (o *CatalogEntityCoralogixApplication) Enabled() bool {
313314
}
314315

315316
/***********************************************************************************************************************
316-
* Coralogix - https://docs.cortex.io/docs/reference/integrations/coralogix
317+
* Checkmarx - https://docs.cortex.io/docs/reference/integrations/checkmarx
317318
**********************************************************************************************************************/
318319

319320
type CatalogEntityCheckmarx struct {
@@ -329,6 +330,27 @@ func (o *CatalogEntityCheckmarx) Enabled() bool {
329330
return len(o.Projects) > 0
330331
}
331332

333+
/***********************************************************************************************************************
334+
* CircleCI - https://docs.cortex.io/docs/reference/integrations/circleci
335+
**********************************************************************************************************************/
336+
337+
type CatalogEntityCircleCi struct {
338+
Projects []CatalogEntityCircleCiProject `json:"projects" yaml:"projects"`
339+
}
340+
341+
func (o *CatalogEntityCircleCi) Enabled() bool {
342+
return len(o.Projects) > 0
343+
}
344+
345+
type CatalogEntityCircleCiProject struct {
346+
Slug string `json:"projectSlug" yaml:"projectSlug"`
347+
Alias string `json:"alias,omitempty" yaml:"alias,omitempty"`
348+
}
349+
350+
func (o *CatalogEntityCircleCiProject) Enabled() bool {
351+
return o.Slug != ""
352+
}
353+
332354
/***********************************************************************************************************************
333355
* CodeCov - https://docs.cortex.io/docs/reference/integrations/codecov
334356
**********************************************************************************************************************/

internal/provider/catalog_entity_resource.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/cortexapps/terraform-provider-cortex/internal/cortex"
78
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -776,6 +777,28 @@ func (r *CatalogEntityResource) Schema(ctx context.Context, req resource.SchemaR
776777
},
777778
},
778779
},
780+
"circle_ci": schema.SingleNestedAttribute{
781+
MarkdownDescription: "CircleCI configuration for the entity.",
782+
Optional: true,
783+
Attributes: map[string]schema.Attribute{
784+
"projects": schema.ListNestedAttribute{
785+
MarkdownDescription: "List of CircleCI projects for the entity.",
786+
Optional: true,
787+
NestedObject: schema.NestedAttributeObject{
788+
Attributes: map[string]schema.Attribute{
789+
"slug": schema.StringAttribute{
790+
MarkdownDescription: "The slug of the project in CircleCI.",
791+
Required: true,
792+
},
793+
"alias": schema.StringAttribute{
794+
MarkdownDescription: "CircleCI alias. Only relevant if you've opted into multi-account support.",
795+
Optional: true,
796+
},
797+
},
798+
},
799+
},
800+
},
801+
},
779802
"coralogix": schema.SingleNestedAttribute{
780803
MarkdownDescription: "Coralogix configuration for the entity.",
781804
Optional: true,

internal/provider/catalog_entity_resource_models.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
8+
79
"github.com/cortexapps/terraform-provider-cortex/internal/cortex"
810
"github.com/hashicorp/terraform-plugin-framework/attr"
911
"github.com/hashicorp/terraform-plugin-framework/diag"
1012
"github.com/hashicorp/terraform-plugin-framework/types"
1113
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1214
"github.com/life4/genesis/slices"
13-
"strings"
1415
)
1516

1617
// CatalogEntityResourceModel describes the resource data model.
@@ -40,6 +41,7 @@ type CatalogEntityResourceModel struct {
4041
CiCd types.Object `tfsdk:"ci_cd"`
4142
BugSnag types.Object `tfsdk:"bug_snag"`
4243
Checkmarx types.Object `tfsdk:"checkmarx"`
44+
CircleCi types.Object `tfsdk:"circle_ci"`
4345
Coralogix types.Object `tfsdk:"coralogix"`
4446
FireHydrant types.Object `tfsdk:"firehydrant"`
4547
K8s types.Object `tfsdk:"k8s"`
@@ -180,6 +182,11 @@ func (o *CatalogEntityResourceModel) ToApiModel(ctx context.Context, diagnostics
180182
if err != nil {
181183
diagnostics.AddError("error parsing Checkmarx configuration", fmt.Sprintf("%+v", err))
182184
}
185+
circleci := CatalogEntityCircleCiResourceModel{}
186+
err = o.CircleCi.As(ctx, &circleci, defaultObjOptions)
187+
if err != nil {
188+
diagnostics.AddError("error parsing CircleCI configuration", fmt.Sprintf("%+v", err))
189+
}
183190
coralogix := CatalogEntityCoralogixResourceModel{}
184191
err = o.Coralogix.As(ctx, &coralogix, defaultObjOptions)
185192
if err != nil {
@@ -270,6 +277,7 @@ func (o *CatalogEntityResourceModel) ToApiModel(ctx context.Context, diagnostics
270277
CiCd: ciCd.ToApiModel(ctx),
271278
BugSnag: bugSnag.ToApiModel(),
272279
Checkmarx: checkmarx.ToApiModel(),
280+
CircleCi: circleci.ToApiModel(),
273281
Coralogix: coralogix.ToApiModel(),
274282
FireHydrant: firehydrant.ToApiModel(),
275283
K8s: k8s.ToApiModel(),
@@ -1405,6 +1413,81 @@ func (o *CatalogEntityCheckmarxProjectResourceModel) FromApiModel(project cortex
14051413
return ob
14061414
}
14071415

1416+
/***********************************************************************************************************************
1417+
* CircleCi
1418+
**********************************************************************************************************************/
1419+
1420+
type CatalogEntityCircleCiResourceModel struct {
1421+
Projects []CatalogEntityCircleCiProjectResourceModel `tfsdk:"projects"`
1422+
}
1423+
1424+
func (o *CatalogEntityCircleCiResourceModel) AttrTypes() map[string]attr.Type {
1425+
ob := CatalogEntityCircleCiProjectResourceModel{}
1426+
return map[string]attr.Type{
1427+
"projects": types.ListType{ElemType: types.ObjectType{AttrTypes: ob.AttrTypes()}},
1428+
}
1429+
}
1430+
1431+
func (o *CatalogEntityCircleCiResourceModel) ToApiModel() cortex.CatalogEntityCircleCi {
1432+
projects := make([]cortex.CatalogEntityCircleCiProject, len(o.Projects))
1433+
for i, p := range o.Projects {
1434+
projects[i] = p.ToApiModel()
1435+
}
1436+
return cortex.CatalogEntityCircleCi{
1437+
Projects: projects,
1438+
}
1439+
}
1440+
1441+
func (o *CatalogEntityCircleCiResourceModel) FromApiModel(ctx context.Context, diagnostics *diag.Diagnostics, entity *cortex.CatalogEntityCircleCi) types.Object {
1442+
obj := CatalogEntityCircleCiResourceModel{}
1443+
if !entity.Enabled() {
1444+
return types.ObjectNull(obj.AttrTypes())
1445+
}
1446+
1447+
projects := make([]CatalogEntityCircleCiProjectResourceModel, len(entity.Projects))
1448+
for i, p := range entity.Projects {
1449+
po := CatalogEntityCircleCiProjectResourceModel{}
1450+
projects[i] = po.FromApiModel(p)
1451+
}
1452+
obj.Projects = projects
1453+
1454+
objectValue, d := types.ObjectValueFrom(ctx, o.AttrTypes(), &obj)
1455+
diagnostics.Append(d...)
1456+
return objectValue
1457+
}
1458+
1459+
type CatalogEntityCircleCiProjectResourceModel struct {
1460+
Slug types.String `tfsdk:"slug"`
1461+
Alias types.String `tfsdk:"alias"`
1462+
}
1463+
1464+
func (o *CatalogEntityCircleCiProjectResourceModel) AttrTypes() map[string]attr.Type {
1465+
return map[string]attr.Type{
1466+
"slug": types.StringType,
1467+
"alias": types.StringType,
1468+
}
1469+
}
1470+
1471+
func (o *CatalogEntityCircleCiProjectResourceModel) ToApiModel() cortex.CatalogEntityCircleCiProject {
1472+
entity := cortex.CatalogEntityCircleCiProject{}
1473+
entity.Slug = o.Slug.ValueString()
1474+
if o.Alias.ValueString() != "" {
1475+
entity.Alias = o.Alias.ValueString()
1476+
}
1477+
return entity
1478+
}
1479+
1480+
func (o *CatalogEntityCircleCiProjectResourceModel) FromApiModel(project cortex.CatalogEntityCircleCiProject) CatalogEntityCircleCiProjectResourceModel {
1481+
ob := CatalogEntityCircleCiProjectResourceModel{}
1482+
ob.Slug = types.StringValue(project.Slug)
1483+
if project.Alias != "" {
1484+
ob.Alias = types.StringValue(project.Alias)
1485+
} else {
1486+
ob.Alias = types.StringNull()
1487+
}
1488+
return ob
1489+
}
1490+
14081491
/***********************************************************************************************************************
14091492
* Coralogix
14101493
**********************************************************************************************************************/

internal/provider/catalog_entity_resource_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package provider_test
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
76
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
89
)
910

1011
func TestAccCatalogEntityResourceMinimal(t *testing.T) {

0 commit comments

Comments
 (0)