Skip to content

Commit

Permalink
Add Monolith sub manifest pkg info resource
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Mar 8, 2023
1 parent 5b4d7f4 commit fb4021b
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 3 deletions.
45 changes: 45 additions & 0 deletions docs/resources/monolith_sub_manifest_pkg_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "zentral_monolith_sub_manifest_pkg_info Resource - terraform-provider-zentral"
subcategory: ""
description: |-
The resource zentral_monolith_sub_manifest_pkg_info manages Monolith sub manifest pkg infos.
---

# zentral_monolith_sub_manifest_pkg_info (Resource)

The resource `zentral_monolith_sub_manifest_pkg_info` manages Monolith sub manifest pkg infos.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `key` (String) Sub manifest key under which this pkg info will be included.
- `pkg_info_name` (String) The name of the pkg info to include.
- `sub_manifest_id` (Number) The `ID` of the sub manifest this pkg info is attached to.

### Optional

- `condition_id` (Number) The `ID` of the condition that is evaluated to decide if this pkg info is included.
- `default_shard` (Number) The default shard value. Defaults to `100`.
- `excluded_tag_ids` (Set of Number) Machines tagged with one of these tags will not receive the pkg info.
- `featured_item` (Boolean) If `true`, this pkg info will be displayed in the featured items section in Managed Software Center. Defaults to `false`.
- `shard_modulo` (Number) The modulo used to calculate the shards. Defaults to `100`.
- `tag_shards` (Attributes Set) A set of tag shard values different from the default shard, to determine if the tagged machines will receive the pkg info. (see [below for nested schema](#nestedatt--tag_shards))

### Read-Only

- `id` (Number) `ID` of the sub manifest pkg info.

<a id="nestedatt--tag_shards"></a>
### Nested Schema for `tag_shards`

Required:

- `shard` (Number) The shard for the tag.
- `tag_id` (Number) The `ID` of the tag.


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
github.com/zentralopensource/goztl v0.1.24
github.com/zentralopensource/goztl v0.1.25
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uU
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
github.com/zentralopensource/goztl v0.1.24 h1:+HhRBRf2hiLM0IKsMSHhqTuYMz+c1EwU8XMMWZsDqow=
github.com/zentralopensource/goztl v0.1.24/go.mod h1:zakEe4QjAUvawFpzehwryMM5f2RI61a8m1wcNxQ8G/A=
github.com/zentralopensource/goztl v0.1.25 h1:TmTlsMl+WW29JEgN9NHd8O26tdia7AODHpVgPHJg/Mw=
github.com/zentralopensource/goztl v0.1.25/go.mod h1:zakEe4QjAUvawFpzehwryMM5f2RI61a8m1wcNxQ8G/A=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
104 changes: 104 additions & 0 deletions internal/provider/monolith_sub_manifest_pkg_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/zentralopensource/goztl"
)

type monolithSubManifestPkgInfo struct {
ID types.Int64 `tfsdk:"id"`
SubManifestID types.Int64 `tfsdk:"sub_manifest_id"`
Key types.String `tfsdk:"key"`
PkgInfoName types.String `tfsdk:"pkg_info_name"`
FeaturedItem types.Bool `tfsdk:"featured_item"`
ConditionID types.Int64 `tfsdk:"condition_id"`
ShardModulo types.Int64 `tfsdk:"shard_modulo"`
DefaultShard types.Int64 `tfsdk:"default_shard"`
ExcludedTagIDs types.Set `tfsdk:"excluded_tag_ids"`
TagShards types.Set `tfsdk:"tag_shards"`
}

var tagShardAttrTypes = map[string]attr.Type{
"tag_id": types.Int64Type,
"shard": types.Int64Type,
}

func monolithSubManifestPkgInfoForState(msmpi *goztl.MonolithSubManifestPkgInfo) monolithSubManifestPkgInfo {
var cID types.Int64
if msmpi.ConditionID != nil {
cID = types.Int64Value(int64(*msmpi.ConditionID))
} else {
cID = types.Int64Null()
}

exTagIDs := make([]attr.Value, 0)
for _, exTagID := range msmpi.ExcludedTagIDs {
exTagIDs = append(exTagIDs, types.Int64Value(int64(exTagID)))
}

tagShards := make([]attr.Value, 0)
for _, tagShard := range msmpi.TagShards {
tagShards = append(
tagShards,
types.ObjectValueMust(
tagShardAttrTypes,
map[string]attr.Value{
"tag_id": types.Int64Value(int64(tagShard.TagID)),
"shard": types.Int64Value(int64(tagShard.Shard)),
},
),
)
}

return monolithSubManifestPkgInfo{
ID: types.Int64Value(int64(msmpi.ID)),
SubManifestID: types.Int64Value(int64(msmpi.SubManifestID)),
Key: types.StringValue(msmpi.Key),
PkgInfoName: types.StringValue(msmpi.PkgInfoName),
FeaturedItem: types.BoolValue(msmpi.FeaturedItem),
ConditionID: cID,
ShardModulo: types.Int64Value(int64(msmpi.ShardModulo)),
DefaultShard: types.Int64Value(int64(msmpi.DefaultShard)),
ExcludedTagIDs: types.SetValueMust(types.Int64Type, exTagIDs),
TagShards: types.SetValueMust(types.ObjectType{AttrTypes: tagShardAttrTypes}, tagShards),
}
}

func monolithSubManifestPkgInfoRequestWithState(data monolithSubManifestPkgInfo) *goztl.MonolithSubManifestPkgInfoRequest {
var cID *int
if !data.ConditionID.IsNull() {
cID = goztl.Int(int(data.ConditionID.ValueInt64()))
}

exTagIDs := make([]int, 0)
for _, exTagID := range data.ExcludedTagIDs.Elements() { // nil if null or unknown → no iterations
exTagIDs = append(exTagIDs, int(exTagID.(types.Int64).ValueInt64()))
}

tagShards := make([]goztl.TagShard, 0)
for _, tagShard := range data.TagShards.Elements() { // nil if null or unknown → no iterations
tagShardMap := tagShard.(types.Object).Attributes()
if tagShardMap != nil {
tagShards = append(
tagShards,
goztl.TagShard{
TagID: int(tagShardMap["tag_id"].(types.Int64).ValueInt64()),
Shard: int(tagShardMap["shard"].(types.Int64).ValueInt64()),
},
)
}
}

return &goztl.MonolithSubManifestPkgInfoRequest{
SubManifestID: int(data.SubManifestID.ValueInt64()),
Key: data.Key.ValueString(),
PkgInfoName: data.PkgInfoName.ValueString(),
FeaturedItem: data.FeaturedItem.ValueBool(),
ConditionID: cID,
ShardModulo: int(data.ShardModulo.ValueInt64()),
DefaultShard: int(data.DefaultShard.ValueInt64()),
ExcludedTagIDs: exTagIDs,
TagShards: tagShards,
}
}
Loading

0 comments on commit fb4021b

Please sign in to comment.