Skip to content

Commit

Permalink
APP-15943 - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jzolo22 committed Nov 8, 2024
1 parent 64485b9 commit 3a4796c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
26 changes: 26 additions & 0 deletions jupiterone/internal/client/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jupiterone/internal/client/rule.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ query GetQuestionRuleInstance($id: ID!) {
}
outputs
tags
labels {
labelName
labelValue
}
}
}

Expand Down
51 changes: 50 additions & 1 deletion jupiterone/resource_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type RuleOperation struct {
}

type RuleLabel struct {
Id types.String `json:"id" tfsdk:"id"`
LabelName types.String `json:"label_name" tfsdk:"label_name"`
LabelValue types.String `json:"label_value" tfsdk:"label_value"`
}
Expand Down Expand Up @@ -274,6 +275,9 @@ func (*QuestionRuleResource) Schema(ctx context.Context, req resource.SchemaRequ
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"label_name": schema.StringAttribute{
Required: true,
Description: "Name of the label",
Expand Down Expand Up @@ -442,6 +446,18 @@ func (r *QuestionRuleResource) Create(ctx context.Context, req resource.CreateRe
data.Id = types.StringValue(c.GetId())
data.Version = types.Int64Value(int64(c.GetVersion()))

if len(data.Labels) > 0 {
labelsWithId := make([]RuleLabel, len(data.Labels))
for i, label := range data.Labels {
labelsWithId[i] = RuleLabel{
Id: types.StringValue(fmt.Sprintf("%s-%s", label.LabelName, label.LabelValue)),
LabelName: label.LabelName,
LabelValue: label.LabelValue,
}
}
data.Labels = labelsWithId
}

tflog.Trace(ctx, "Created rule",
map[string]interface{}{"title": data.Name, "id": data.Id})

Expand Down Expand Up @@ -539,6 +555,23 @@ func (r *QuestionRuleResource) Read(ctx context.Context, req resource.ReadReques
resp.Diagnostics.AddError("error unmarshaling templates from response", err.Error())
}

if len(data.Labels) > 0 {
labelsWithId := make([]RuleLabel, len(data.Labels))
for i, label := range data.Labels {
if label.Id.IsNull() {
labelsWithId[i] = RuleLabel{
Id: types.StringValue(fmt.Sprintf("%s-%s", label.LabelName, label.LabelValue)), // Generate a unique ID based on labelName and labelValue
LabelName: label.LabelName,
LabelValue: label.LabelValue,
}
} else {
// If the label already has an ID, keep it
labelsWithId[i] = label
}
}
data.Labels = labelsWithId
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -601,6 +634,23 @@ func (r *QuestionRuleResource) Update(ctx context.Context, req resource.UpdateRe

data.Version = types.Int64Value(int64(update.GetVersion()))

if len(data.Labels) > 0 {
labelsWithId := make([]RuleLabel, len(data.Labels))
for i, label := range data.Labels {
if label.Id.IsNull() {
labelsWithId[i] = RuleLabel{
Id: types.StringValue(fmt.Sprintf("%s-%s", label.LabelName, label.LabelValue)), // Generate a unique ID based on labelName and labelValue
LabelName: label.LabelName,
LabelValue: label.LabelValue,
}
} else {
// If the label already has an ID, keep it
labelsWithId[i] = label
}
}
data.Labels = labelsWithId
}

tflog.Trace(ctx, "Updated rule",
map[string]interface{}{"title": data.Name, "id": data.Id})

Expand Down Expand Up @@ -747,7 +797,6 @@ func (r *RuleModel) BuildCreateInlineQuestionRuleInstanceInput() (client.CreateI
NotifyOnFailure: r.NotifyOnFailure.ValueBool(),
TriggerActionsOnNewEntitiesOnly: r.TriggerOnNewOnly.ValueBool(),
IgnorePreviousResults: r.IgnorePreviousResults.ValueBool(),
// Labels: r.Labels,
}

var err error
Expand Down

0 comments on commit 3a4796c

Please sign in to comment.