diff --git a/docs/data-sources/foreman_katello_lifecycle_environment.md b/docs/data-sources/foreman_katello_lifecycle_environment.md new file mode 100755 index 00000000..084f2b49 --- /dev/null +++ b/docs/data-sources/foreman_katello_lifecycle_environment.md @@ -0,0 +1,36 @@ + +# foreman_katello_lifecycle_environment + + +Lifecycle environments group hosts into logical stages, example dev/test/prod. + + +## Example Usage + +``` +# Autogenerated example with required keys +data "foreman_katello_lifecycle_environment" "example" { + name = "Library" +} +``` + + +## Argument Reference + +The following arguments are supported: + +- `name` - (Required) Name of the lifecycle environment. + + +## Attributes Reference + +The following attributes are exported: + +- `description` - Description for the lifecycle environment +- `label` - Label for the lifecycle environment. Cannot be changed after creation. By default set to the name, with underscores as spaces replacement. +- `library` - Specifies if this environment is the special 'Library' root environment. +- `name` - Name of the lifecycle environment. +- `organization_id` - +- `prior_id` - ID of the prior lifecycle environment. Use '1' to refer to the built-in 'Library' root environment. +- `successor_id` - + diff --git a/docs/resources/foreman_katello_lifecycle_environment.md b/docs/resources/foreman_katello_lifecycle_environment.md new file mode 100755 index 00000000..efb047e9 --- /dev/null +++ b/docs/resources/foreman_katello_lifecycle_environment.md @@ -0,0 +1,42 @@ + +# foreman_katello_lifecycle_environment + + +Lifecycle environments group hosts into logical stages, example dev/test/prod. + + +## Example Usage + +``` +# Autogenerated example with required keys +resource "foreman_katello_lifecycle_environment" "example" { + name = "My new env" + organization_id = 1 + prior_id = data.foreman_katello_lifecycle_environment.library.id +} +``` + + +## Argument Reference + +The following arguments are supported: + +- `description` - (Optional) Description for the lifecycle environment +- `label` - (Optional, Force New) Label for the lifecycle environment. Cannot be changed after creation. By default set to the name, with underscores as spaces replacement. +- `name` - (Required) Name of the lifecycle environment. +- `organization_id` - (Required) +- `prior_id` - (Required) ID of the prior lifecycle environment. Use '1' to refer to the built-in 'Library' root environment. + + +## Attributes Reference + +The following attributes are exported: + +- `description` - Description for the lifecycle environment +- `label` - Label for the lifecycle environment. Cannot be changed after creation. By default set to the name, with underscores as spaces replacement. +- `library` - Specifies if this environment is the special 'Library' root environment. +- `name` - Name of the lifecycle environment. +- `organization_id` - +- `prior_id` - ID of the prior lifecycle environment. Use '1' to refer to the built-in 'Library' root environment. +- `successor_id` - + diff --git a/examples/lifecycle_environment/main.tf b/examples/lifecycle_environment/main.tf new file mode 100644 index 00000000..bfdf054c --- /dev/null +++ b/examples/lifecycle_environment/main.tf @@ -0,0 +1,11 @@ +// Get the root 'Library' environment as data source +data "foreman_katello_lifecycle_environment" "library" { + name = "Library" +} + +// Then create a new lifecycle environment which uses the Library as the prior environment +resource "foreman_katello_lifecycle_environment" "newenv" { + name = "My new lifecycle env" + prior_id = data.foreman_katello_lifecycle_environment.library.id + organization_id = 1 +} diff --git a/foreman/api/client.go b/foreman/api/client.go index 0522f1af..2ca4155d 100644 --- a/foreman/api/client.go +++ b/foreman/api/client.go @@ -218,6 +218,8 @@ func (client *Client) NewRequestWithContext(ctx context.Context, method string, // Check for katello endpoint if strings.HasPrefix(endpoint, "katello") { reqURL.Path = FOREMAN_KATELLO_API_URL_PREFIX + strings.TrimPrefix(endpoint, "katello") + } else if strings.HasPrefix(endpoint, "/katello/api") { + reqURL.Path = endpoint } else if strings.HasPrefix(endpoint, "puppet") { reqURL.Path = FOREMAN_PUPPET_API_URL_PREFIX + strings.TrimPrefix(endpoint, "puppet") } else { diff --git a/foreman/api/katello_lifecycle_environments.go b/foreman/api/katello_lifecycle_environments.go new file mode 100644 index 00000000..7d5a73d1 --- /dev/null +++ b/foreman/api/katello_lifecycle_environments.go @@ -0,0 +1,205 @@ +package api + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "github.com/terraform-coop/terraform-provider-foreman/foreman/utils" + "net/http" +) + +const ( + LifecycleEnvironmentEndpointPrefix = "/katello/api/environments" + LifecycleEnvironmentById = LifecycleEnvironmentEndpointPrefix + "/%d" // :id + LifecycleEnvironmentPathsByOrg = "/katello/api/organizations/%d/environments/paths" // :organization_id +) + +type ContentViews struct { + Name string `json:"name"` + Id int `json:"id"` +} + +type Organization struct { + Name string `json:"name"` + Label string `json:"label"` + Id int `json:"id"` +} + +type LifecycleEnvironment struct { + ForemanObject + + Label string `json:"label"` + Description string `json:"description"` + OrganizationId int `json:"organization_id"` + Organization Organization `json:"organization"` + + // Is this LifecycleEnvironment a library? + Library bool `json:"library"` + + // Container Image Registry related + RegistryNamePattern string `json:"registry_name_pattern"` + RegistryUnauthenticatedPull bool `json:"registry_unauthenticated_pull"` + + Prior struct { + Name string `json:"name"` + Id int `json:"id"` + } `json:"prior"` + + Successor struct { + Name string `json:"name"` + Id int `json:"id"` + } `json:"successor"` + + Counts struct { + ContentHosts int `json:"content_hosts"` + ContentViews int `json:"content_views"` + } `json:"counts"` + + ContentViews []ContentViews `json:"content_views"` +} + +func (lce *LifecycleEnvironment) MarshalJSON() ([]byte, error) { + jsonMap := map[string]interface{}{ + "id": lce.Id, + "name": lce.Name, + "description": lce.Description, + "organization_id": lce.OrganizationId, + "label": lce.Label, + "prior_id": lce.Prior.Id, + } + return json.Marshal(jsonMap) +} + +func (c *Client) QueryLifecycleEnvironment(ctx context.Context, d *LifecycleEnvironment) (QueryResponse, error) { + utils.TraceFunctionCall() + + queryResponse := QueryResponse{} + + endpoint := LifecycleEnvironmentEndpointPrefix + req, err := c.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) + if err != nil { + return queryResponse, err + } + + // dynamically build the query based on the attributes + reqQuery := req.URL.Query() + name := `"` + d.Name + `"` + reqQuery.Set("search", "name="+name) + + req.URL.RawQuery = reqQuery.Encode() + err = c.SendAndParse(req, &queryResponse) + if err != nil { + return queryResponse, err + } + + utils.Debugf("queryResponse: %+v", queryResponse) + + var results []LifecycleEnvironment + resultsBytes, err := json.Marshal(queryResponse.Results) + if err != nil { + return queryResponse, err + } + err = json.Unmarshal(resultsBytes, &results) + if err != nil { + return queryResponse, err + } + + // convert the search results from []ForemanImage to []interface + // and set the search results on the query + iArr := make([]interface{}, len(results)) + for idx, val := range results { + iArr[idx] = val + } + queryResponse.Results = iArr + + return queryResponse, nil +} + +func (c *Client) CreateKatelloLifecycleEnvironment(ctx context.Context, lce *LifecycleEnvironment) (*LifecycleEnvironment, error) { + utils.TraceFunctionCall() + + endpoint := LifecycleEnvironmentEndpointPrefix + + jsonBytes, err := c.WrapJSONWithTaxonomy(nil, lce) + if err != nil { + return nil, err + } + + req, err := c.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, err + } + + var createdLce LifecycleEnvironment + err = c.SendAndParse(req, &createdLce) + if err != nil { + return nil, err + } + + utils.Debugf("createdLce: %+v", createdLce) + + return &createdLce, nil +} + +func (c *Client) ReadKatelloLifecycleEnvironment(ctx context.Context, d *LifecycleEnvironment) (*LifecycleEnvironment, error) { + utils.TraceFunctionCall() + + reqEndpoint := fmt.Sprintf(LifecycleEnvironmentById, d.Id) + var lce LifecycleEnvironment + + req, err := c.NewRequestWithContext(ctx, http.MethodGet, reqEndpoint, nil) + if err != nil { + return nil, err + } + + err = c.SendAndParse(req, &lce) + if err != nil { + return nil, err + } + + utils.Debugf("read LifecycleEnv: %+v", lce) + + return &lce, nil +} + +func (c *Client) UpdateKatelloLifecycleEnvironment(ctx context.Context, lce *LifecycleEnvironment) (*LifecycleEnvironment, error) { + utils.TraceFunctionCall() + + endpoint := fmt.Sprintf(LifecycleEnvironmentById, lce.Id) + + jsonBytes, err := c.WrapJSONWithTaxonomy(nil, lce) + if err != nil { + return nil, err + } + + utils.Debugf("jsonBytes: %s", jsonBytes) + + req, err := c.NewRequestWithContext(ctx, http.MethodPut, endpoint, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, err + } + + var updatedLce LifecycleEnvironment + err = c.SendAndParse(req, &updatedLce) + if err != nil { + return nil, err + } + + utils.Debugf("updatedLce: %+v", updatedLce) + + return &updatedLce, nil +} + +func (c *Client) DeleteKatelloLifecycleEnvironment(ctx context.Context, id int) error { + utils.TraceFunctionCall() + + endpoint := fmt.Sprintf(LifecycleEnvironmentById, id) + + req, err := c.NewRequestWithContext(ctx, http.MethodDelete, endpoint, nil) + if err != nil { + return err + } + + return c.SendAndParse(req, nil) +} diff --git a/foreman/data_source_foreman_katello_lifecycle_environment.go b/foreman/data_source_foreman_katello_lifecycle_environment.go new file mode 100644 index 00000000..6c72c32d --- /dev/null +++ b/foreman/data_source_foreman_katello_lifecycle_environment.go @@ -0,0 +1,65 @@ +package foreman + +import ( + "context" + "fmt" + "github.com/HanseMerkur/terraform-provider-utils/autodoc" + "github.com/HanseMerkur/terraform-provider-utils/helper" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/terraform-coop/terraform-provider-foreman/foreman/api" + "github.com/terraform-coop/terraform-provider-foreman/foreman/utils" +) + +func dataSourceForemanKatelloLifecycleEnvironment() *schema.Resource { + r := resourceForemanKatelloLifecycleEnvironment() + ds := helper.DataSourceSchemaFromResourceSchema(r.Schema) + + // define searchable attributes for the data source + ds["name"] = &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: fmt.Sprintf("Name of the lifecycle environment. %s \"Library\"", autodoc.MetaExample), + } + + return &schema.Resource{ + ReadContext: dataSourceForemanKatelloLifecycleRead, + Schema: ds, + } +} + +func dataSourceForemanKatelloLifecycleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + utils.TraceFunctionCall() + + client := meta.(*api.Client) + lce := buildForemanKatelloLifecycleEnvironment(d) + + utils.Debugf("lifecycle env: %+v", lce) + + queryResponse, err := client.QueryLifecycleEnvironment(ctx, lce) + if err != nil { + return diag.FromErr(err) + } + + if queryResponse.Subtotal == 0 { + return diag.Errorf("data source lifecycle_environment returned no results") + } else if queryResponse.Subtotal > 1 { + return diag.Errorf("data source lifecycle_environment returned more than 1 result") + } + + if queryLce, ok := queryResponse.Results[0].(api.LifecycleEnvironment); !ok { + return diag.Errorf( + "data source results contain unexpected type. Expected "+ + "[api.LifecycleEnvironment], got [%T]", + queryResponse.Results[0], + ) + } else { + lce = &queryLce + } + + utils.Debugf("lifecycle env: %+v", lce) + + setResourceDataFromForemanKatelloLifecycleEnvironment(d, lce) + + return nil +} diff --git a/foreman/provider.go b/foreman/provider.go index 2ae1b1fc..75617280 100644 --- a/foreman/provider.go +++ b/foreman/provider.go @@ -176,67 +176,69 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ - "foreman_architecture": resourceForemanArchitecture(), - "foreman_host": resourceForemanHost(), - "foreman_hostgroup": resourceForemanHostgroup(), - "foreman_media": resourceForemanMedia(), - "foreman_model": resourceForemanModel(), - "foreman_operatingsystem": resourceForemanOperatingSystem(), - "foreman_partitiontable": resourceForemanPartitionTable(), - "foreman_provisioningtemplate": resourceForemanProvisioningTemplate(), - "foreman_smartproxy": resourceForemanSmartProxy(), - "foreman_computeresource": resourceForemanComputeResource(), - "foreman_image": resourceForemanImage(), - "foreman_environment": resourceForemanEnvironment(), - "foreman_parameter": resourceForemanParameter(), - "foreman_global_parameter": resourceForemanCommonParameter(), - "foreman_subnet": resourceForemanSubnet(), - "foreman_domain": resourceForemanDomain(), - "foreman_defaulttemplate": resourceForemanDefaultTemplate(), - "foreman_httpproxy": resourceForemanHTTPProxy(), - "foreman_katello_content_credential": resourceForemanKatelloContentCredential(), - "foreman_katello_product": resourceForemanKatelloProduct(), - "foreman_katello_repository": resourceForemanKatelloRepository(), - "foreman_katello_sync_plan": resourceForemanKatelloSyncPlan(), - "foreman_user": resourceForemanUser(), - "foreman_usergroup": resourceForemanUsergroup(), - "foreman_override_value": resourceForemanOverrideValue(), - "foreman_computeprofile": resourceForemanComputeProfile(), - "foreman_jobtemplate": resourceForemanJobTemplate(), - "foreman_templateinput": resourceForemanTemplateInput(), + "foreman_architecture": resourceForemanArchitecture(), + "foreman_host": resourceForemanHost(), + "foreman_hostgroup": resourceForemanHostgroup(), + "foreman_media": resourceForemanMedia(), + "foreman_model": resourceForemanModel(), + "foreman_operatingsystem": resourceForemanOperatingSystem(), + "foreman_partitiontable": resourceForemanPartitionTable(), + "foreman_provisioningtemplate": resourceForemanProvisioningTemplate(), + "foreman_smartproxy": resourceForemanSmartProxy(), + "foreman_computeresource": resourceForemanComputeResource(), + "foreman_image": resourceForemanImage(), + "foreman_environment": resourceForemanEnvironment(), + "foreman_parameter": resourceForemanParameter(), + "foreman_global_parameter": resourceForemanCommonParameter(), + "foreman_subnet": resourceForemanSubnet(), + "foreman_domain": resourceForemanDomain(), + "foreman_defaulttemplate": resourceForemanDefaultTemplate(), + "foreman_httpproxy": resourceForemanHTTPProxy(), + "foreman_katello_content_credential": resourceForemanKatelloContentCredential(), + "foreman_katello_lifecycle_environment": resourceForemanKatelloLifecycleEnvironment(), + "foreman_katello_product": resourceForemanKatelloProduct(), + "foreman_katello_repository": resourceForemanKatelloRepository(), + "foreman_katello_sync_plan": resourceForemanKatelloSyncPlan(), + "foreman_user": resourceForemanUser(), + "foreman_usergroup": resourceForemanUsergroup(), + "foreman_override_value": resourceForemanOverrideValue(), + "foreman_computeprofile": resourceForemanComputeProfile(), + "foreman_jobtemplate": resourceForemanJobTemplate(), + "foreman_templateinput": resourceForemanTemplateInput(), }, DataSourcesMap: map[string]*schema.Resource{ - "foreman_architecture": dataSourceForemanArchitecture(), - "foreman_domain": dataSourceForemanDomain(), - "foreman_environment": dataSourceForemanEnvironment(), - "foreman_hostgroup": dataSourceForemanHostgroup(), - "foreman_media": dataSourceForemanMedia(), - "foreman_model": dataSourceForemanModel(), - "foreman_operatingsystem": dataSourceForemanOperatingSystem(), - "foreman_partitiontable": dataSourceForemanPartitionTable(), - "foreman_provisioningtemplate": dataSourceForemanProvisioningTemplate(), - "foreman_puppetclass": dataSourceForemanPuppetClass(), - "foreman_smartclassparameter": dataSourceForemanSmartClassParameter(), - "foreman_smartproxy": dataSourceForemanSmartProxy(), - "foreman_subnet": dataSourceForemanSubnet(), - "foreman_templatekind": dataSourceForemanTemplateKind(), - "foreman_computeprofile": dataSourceForemanComputeProfile(), - "foreman_computeresource": dataSourceForemanComputeResource(), - "foreman_image": dataSourceForemanImage(), - "foreman_parameter": dataSourceForemanParameter(), - "foreman_global_parameter": dataSourceForemanCommonParameter(), - "foreman_defaulttemplate": dataSourceForemanDefaultTemplate(), - "foreman_httpproxy": dataSourceForemanHTTPProxy(), - "foreman_katello_content_credential": dataSourceForemanKatelloContentCredential(), - "foreman_katello_product": dataSourceForemanKatelloProduct(), - "foreman_katello_repository": dataSourceForemanKatelloRepository(), - "foreman_katello_sync_plan": dataSourceForemanKatelloSyncPlan(), - "foreman_user": dataSourceForemanUser(), - "foreman_usergroup": dataSourceForemanUsergroup(), - "foreman_setting": dataSourceForemanSetting(), - "foreman_jobtemplate": dataSourceForemanJobTemplate(), - "foreman_templateinput": dataSourceForemanTemplateInput(), + "foreman_architecture": dataSourceForemanArchitecture(), + "foreman_domain": dataSourceForemanDomain(), + "foreman_environment": dataSourceForemanEnvironment(), + "foreman_hostgroup": dataSourceForemanHostgroup(), + "foreman_media": dataSourceForemanMedia(), + "foreman_model": dataSourceForemanModel(), + "foreman_operatingsystem": dataSourceForemanOperatingSystem(), + "foreman_partitiontable": dataSourceForemanPartitionTable(), + "foreman_provisioningtemplate": dataSourceForemanProvisioningTemplate(), + "foreman_puppetclass": dataSourceForemanPuppetClass(), + "foreman_smartclassparameter": dataSourceForemanSmartClassParameter(), + "foreman_smartproxy": dataSourceForemanSmartProxy(), + "foreman_subnet": dataSourceForemanSubnet(), + "foreman_templatekind": dataSourceForemanTemplateKind(), + "foreman_computeprofile": dataSourceForemanComputeProfile(), + "foreman_computeresource": dataSourceForemanComputeResource(), + "foreman_image": dataSourceForemanImage(), + "foreman_parameter": dataSourceForemanParameter(), + "foreman_global_parameter": dataSourceForemanCommonParameter(), + "foreman_defaulttemplate": dataSourceForemanDefaultTemplate(), + "foreman_httpproxy": dataSourceForemanHTTPProxy(), + "foreman_katello_content_credential": dataSourceForemanKatelloContentCredential(), + "foreman_katello_lifecycle_environment": dataSourceForemanKatelloLifecycleEnvironment(), + "foreman_katello_product": dataSourceForemanKatelloProduct(), + "foreman_katello_repository": dataSourceForemanKatelloRepository(), + "foreman_katello_sync_plan": dataSourceForemanKatelloSyncPlan(), + "foreman_user": dataSourceForemanUser(), + "foreman_usergroup": dataSourceForemanUsergroup(), + "foreman_setting": dataSourceForemanSetting(), + "foreman_jobtemplate": dataSourceForemanJobTemplate(), + "foreman_templateinput": dataSourceForemanTemplateInput(), }, ConfigureContextFunc: providerConfigure, } diff --git a/foreman/resource_foreman_katello_lifecycle_environment.go b/foreman/resource_foreman_katello_lifecycle_environment.go new file mode 100644 index 00000000..cac42e3f --- /dev/null +++ b/foreman/resource_foreman_katello_lifecycle_environment.go @@ -0,0 +1,183 @@ +package foreman + +import ( + "context" + "fmt" + "github.com/HanseMerkur/terraform-provider-utils/autodoc" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/terraform-coop/terraform-provider-foreman/foreman/api" + "github.com/terraform-coop/terraform-provider-foreman/foreman/utils" + "strconv" +) + +func resourceForemanKatelloLifecycleEnvironment() *schema.Resource { + return &schema.Resource{ + + CreateContext: resourceForemanKatelloLifecycleEnvironmentCreate, + ReadContext: resourceForemanKatelloLifecycleEnvironmentRead, + UpdateContext: resourceForemanKatelloLifecycleEnvironmentUpdate, + DeleteContext: resourceForemanKatelloLifecycleEnvironmentDelete, + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + /* + Left over, not implemented yet: + + RegistryNamePattern string `json:"registry_name_pattern"` + RegistryUnauthenticatedPull bool `json:"registry_unauthenticated_pull"` + + Counts struct { + ContentHosts int `json:"content_hosts"` + ContentViews int `json:"content_views"` + } `json:"counts"` + + ContentViews []ContentViews `json:"content_views"` + */ + + Schema: map[string]*schema.Schema{ + autodoc.MetaAttribute: { + Type: schema.TypeBool, + Computed: true, + Description: fmt.Sprintf( + "%s Lifecycle environments group hosts into logical stages, example dev/test/prod.", + autodoc.MetaSummary, + ), + }, + "name": { + Type: schema.TypeString, + Required: true, + Description: fmt.Sprintf("Name of the lifecycle environment. %s \"My new env\"", autodoc.MetaExample), + }, + "description": { + Type: schema.TypeString, + Optional: true, + Description: "Description for the lifecycle environment", + }, + "label": { + Type: schema.TypeString, + Optional: true, + Computed: true, // Created from name if not passed in + ForceNew: true, + Description: fmt.Sprintf( + "Label for the lifecycle environment. Cannot be changed after creation. "+ + "By default set to the name, with underscores as spaces replacement. %s", + autodoc.MetaExample, + ), + }, + "organization_id": { + Type: schema.TypeInt, + Required: true, + Description: fmt.Sprintf("%s 1", autodoc.MetaExample), + }, + "library": { + Type: schema.TypeBool, + Computed: true, + Description: "Specifies if this environment is the special 'Library' root environment.", + }, + "prior_id": { + Type: schema.TypeInt, + Required: true, + Description: fmt.Sprintf("ID of the prior lifecycle environment. Use '1' to refer to "+ + "the built-in 'Library' root environment. "+ + "%s data.foreman_katello_lifecycle_environment.library.id", autodoc.MetaExample), + }, + "successor_id": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func buildForemanKatelloLifecycleEnvironment(d *schema.ResourceData) *api.LifecycleEnvironment { + utils.TraceFunctionCall() + + lce := api.LifecycleEnvironment{} + lce.ForemanObject = *buildForemanObject(d) + + lce.Description = d.Get("description").(string) + lce.Label = d.Get("label").(string) + lce.OrganizationId = d.Get("organization_id").(int) + lce.Library = d.Get("library").(bool) + lce.Prior.Id = d.Get("prior_id").(int) + lce.Successor.Id = d.Get("successor_id").(int) + + return &lce +} + +func setResourceDataFromForemanKatelloLifecycleEnvironment(d *schema.ResourceData, lce *api.LifecycleEnvironment) { + utils.TraceFunctionCall() + + d.SetId(strconv.Itoa(lce.Id)) + d.Set("name", lce.Name) + d.Set("description", lce.Description) + d.Set("label", lce.Label) + d.Set("organization_id", lce.OrganizationId) + d.Set("library", lce.Library) + d.Set("prior_id", lce.Prior.Id) + d.Set("successor_id", lce.Successor.Id) +} + +func resourceForemanKatelloLifecycleEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + utils.TraceFunctionCall() + + client := meta.(*api.Client) + lce := buildForemanKatelloLifecycleEnvironment(d) + utils.Debugf("lce: %+v", lce) + + createdLce, err := client.CreateKatelloLifecycleEnvironment(ctx, lce) + if err != nil { + return diag.FromErr(err) + } + utils.Debugf("Created lce: %+v", createdLce) + + setResourceDataFromForemanKatelloLifecycleEnvironment(d, createdLce) + return nil +} + +func resourceForemanKatelloLifecycleEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + utils.TraceFunctionCall() + + client := meta.(*api.Client) + lce := buildForemanKatelloLifecycleEnvironment(d) + + readLce, readErr := client.ReadKatelloLifecycleEnvironment(ctx, lce) + if readErr != nil { + return diag.FromErr(api.CheckDeleted(d, readErr)) + } + utils.Debugf("Read lifecycle env: %+v", readLce) + + setResourceDataFromForemanKatelloLifecycleEnvironment(d, readLce) + return nil +} + +func resourceForemanKatelloLifecycleEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + utils.TraceFunctionCall() + + client := meta.(*api.Client) + lce := buildForemanKatelloLifecycleEnvironment(d) + utils.Debugf("lce: [%+v]", lce) + + updatedLce, err := client.UpdateKatelloLifecycleEnvironment(ctx, lce) + if err != nil { + return diag.FromErr(err) + } + utils.Debugf("updatedLce: %+v", updatedLce) + + setResourceDataFromForemanKatelloLifecycleEnvironment(d, updatedLce) + return nil +} + +func resourceForemanKatelloLifecycleEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + utils.TraceFunctionCall() + + client := meta.(*api.Client) + lce := buildForemanKatelloLifecycleEnvironment(d) + + utils.Debugf("lce to be deleted: %+v", lce) + + return diag.FromErr(api.CheckDeleted(d, client.DeleteKatelloLifecycleEnvironment(ctx, lce.Id))) +} diff --git a/foreman/utils/utils.go b/foreman/utils/utils.go index aca266cd..7604a8b4 100644 --- a/foreman/utils/utils.go +++ b/foreman/utils/utils.go @@ -11,6 +11,11 @@ func Debug(format string, a ...interface{}) { // Removed in branch feat/job_templates, to be filled in separate branch } +func Debugf(format string, a ...interface{}) { + // Placeholder in branch feat/katello_lifecycleenvs, to be filled by PR #146 + // https://github.com/terraform-coop/terraform-provider-foreman/pull/146 +} + // Prints line and file and then exits with fatal error message func Fatalf(format string, a ...interface{}) { // Removed in branch feat/job_templates, to be filled in separate branch