This commit is contained in:
2023-12-18 18:36:55 +03:00
parent 294680282e
commit e2ee45ee14
155 changed files with 10125 additions and 17209 deletions

View File

@@ -1,140 +1,69 @@
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)
func dataSourcePcideviceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pcidevice, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
d.Set("ckey", pcidevice.CKey)
d.Set("meta", flattens.FlattenMeta(pcidevice.Meta))
d.Set("compute_id", pcidevice.ComputeID)
d.Set("description", pcidevice.Description)
d.Set("guid", pcidevice.GUID)
d.Set("hw_path", pcidevice.HwPath)
d.Set("device_id",pcidevice.ID)
d.Set("name", pcidevice.Name)
d.Set("rg_id", pcidevice.RGID)
d.Set("stack_id", pcidevice.StackID)
d.Set("status", pcidevice.Status)
d.Set("system_name", pcidevice.SystemName)
d.SetId(strconv.Itoa(d.Get("device_id").(int)))
return nil
}
func dataSourcePcideviceSchemaMake() map[string]*schema.Schema {
rets := map[string]*schema.Schema{
"device_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"ckey": {
Type: schema.TypeString,
Computed: true,
},
"meta": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"compute_id": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"hw_path": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"rg_id": {
Type: schema.TypeInt,
Computed: true,
},
"stack_id": {
Type: schema.TypeInt,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"system_name": {
Type: schema.TypeString,
Computed: true,
},
}
return rets
}
func DataSourcePcidevice() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourcePcideviceRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourcePcideviceSchemaMake(),
}
}
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
)
func dataSourcePcideviceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pcidevice, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
flattenPcidevice(d, pcidevice)
d.SetId(strconv.Itoa(d.Get("device_id").(int)))
return nil
}
func DataSourcePcidevice() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourcePcideviceRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourcePcideviceSchemaMake(),
}
}

View File

@@ -1,149 +1,72 @@
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/pcidevice"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)
func flattenPcideviceList(pl *pcidevice.ListPCIDevices) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(pl.Data))
for _, item := range pl.Data {
temp := map[string]interface{}{
"ckey": item.CKey,
"meta": flattens.FlattenMeta(item.Meta),
"compute_id": item.ComputeID,
"description": item.Description,
"guid": item.GUID,
"hw_path": item.HwPath,
"device_id": item.ID,
"rg_id": item.RGID,
"name": item.Name,
"stack_id": item.StackID,
"status": item.Status,
"system_name": item.SystemName,
}
res = append(res, temp)
}
return res
}
func dataSourcePcideviceListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pcideviceList, err := utilityPcideviceListCheckPresence(ctx, m)
if err != nil {
return diag.FromErr(err)
}
d.Set("items", flattenPcideviceList(pcideviceList))
d.Set("entry_count", pcideviceList.EntryCount)
id := uuid.New()
d.SetId(id.String())
return nil
}
func dataSourcePcideviceListSchemaMake() map[string]*schema.Schema {
rets := map[string]*schema.Schema{
"by_id": {
Type: schema.TypeInt,
Optional: true,
Description: "by_id",
},
"compute_id": {
Type: schema.TypeInt,
Optional: true,
Description: "compute_id",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "name",
},
"rg_id": {
Type: schema.TypeInt,
Optional: true,
Description: "rg_id",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "status",
},
"page": {
Type: schema.TypeInt,
Optional: true,
Description: "page number",
},
"size": {
Type: schema.TypeInt,
Optional: true,
Description: "page size",
},
"items": {
Type: schema.TypeList,
Computed: true,
Description: "pcidevice list",
Elem: &schema.Resource{
Schema: dataSourcePcideviceSchemaMake(),
},
},
"entry_count": {
Type: schema.TypeInt,
Computed: true,
Description: "entry count",
},
}
return rets
}
func DataSourcePcideviceList() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourcePcideviceListRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourcePcideviceListSchemaMake(),
}
}
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
)
func dataSourcePcideviceListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pcideviceList, err := utilityPcideviceListCheckPresence(ctx, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
d.Set("items", flattenPcideviceList(pcideviceList))
d.Set("entry_count", pcideviceList.EntryCount)
id := uuid.New()
d.SetId(id.String())
return nil
}
func DataSourcePcideviceList() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourcePcideviceListRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourcePcideviceListSchemaMake(),
}
}

View File

@@ -34,55 +34,26 @@ package pcidevice
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/rg"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/service/cloudbroker/ic"
)
func existStackID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
c := m.(*controller.ControllerCfg)
StackID := uint64(d.Get("stack_id").(int))
RGID := uint64(d.Get("rg_id").(int))
func checkParamsExistence(ctx context.Context, d *schema.ResourceData, c *controller.ControllerCfg) diag.Diagnostics {
var errs []error
req := rg.ListRequest{
IncludeDeleted: false,
stackId := uint64(d.Get("stack_id").(int))
rgId := uint64(d.Get("rd_id").(int))
if err := ic.ExistRG(ctx, rgId, c); err != nil {
errs = append(errs, err)
}
rgList, err := c.CloudBroker().RG().List(ctx, req)
if err != nil {
return false, err
if err := ic.ExistStackInPcidevice(ctx, stackId, rgId, c); err != nil {
errs = append(errs, err)
}
for _, v := range rgList.FilterByID(RGID).Data {
for _, idVM := range v.VMs {
req := compute.GetRequest{
ComputeID: idVM,
}
checkStackID, err := c.CloudBroker().Compute().Get(ctx, req)
if err != nil {
return false, err
}
if checkStackID.StackID == StackID {
return true, nil
}
}
}
return false, err
}
func existRGID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
c := m.(*controller.ControllerCfg)
RGID := uint64(d.Get("rg_id").(int))
req := rg.ListRequest{
IncludeDeleted: false,
}
rgList, err := c.CloudBroker().RG().List(ctx, req)
if err != nil {
return false, err
}
return len(rgList.FilterByID(RGID).Data) != 0, nil
return dc.ErrorsToDiagnostics(errs)
}

View File

@@ -1,292 +1,229 @@
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/pcidevice"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/status"
)
func resourcePcideviceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceCreate: called for pcidevice %s", d.Get("name").(string))
c := m.(*controller.ControllerCfg)
req := pcidevice.CreateRequest{}
haveRGID, err := existRGID(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if !haveRGID {
return diag.Errorf("resourcePcideviceCreate: can't create Pcidevice because RGID %d is not allowed or does not exist", d.Get("rg_id").(int))
}
haveStackID, err := existStackID(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if !haveStackID {
return diag.Errorf("resourcePcideviceCreate: can't create Pcidevice because StackID %d is not allowed or does not exist", d.Get("stack_id").(int))
}
req.StackID = uint64(d.Get("stack_id").(int))
req.RGID = uint64(d.Get("rg_id").(int))
req.Name = d.Get("name").(string)
req.HWPath = d.Get("hw_path").(string)
if description, ok := d.GetOk("description"); ok {
req.Description = description.(string)
}
pcideviceId, err := c.CloudBroker().PCIDevice().Create(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatUint(pcideviceId, 10))
d.Set("device_id", pcideviceId)
return resourcePcideviceRead(ctx, d, m)
}
func resourcePcideviceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pcidevice, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
d.Set("ckey", pcidevice.CKey)
d.Set("meta", flattens.FlattenMeta(pcidevice.Meta))
d.Set("compute_id", pcidevice.ComputeID)
d.Set("description", pcidevice.Description)
d.Set("guid", pcidevice.GUID)
d.Set("hw_path", pcidevice.HwPath)
d.Set("device_id", pcidevice.ID)
d.Set("rg_id", pcidevice.RGID)
d.Set("name", pcidevice.Name)
d.Set("stack_id", pcidevice.StackID)
d.Set("status", pcidevice.Status)
d.Set("system_name", pcidevice.SystemName)
return nil
}
func resourcePcideviceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
haveRGID, err := existRGID(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if !haveRGID {
return diag.Errorf("resourcePcideviceCreate: can't update Pcidevice because RGID %d is not allowed or does not exist", d.Get("rg_id").(int))
}
haveStackID, err := existStackID(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if !haveStackID {
return diag.Errorf("resourcePcideviceCreate: can't update Pcidevice because StackID %d is not allowed or does not exist", d.Get("stack_id").(int))
}
if d.HasChange("enable") {
state := d.Get("enable").(bool)
c := m.(*controller.ControllerCfg)
if state {
req := pcidevice.EnableRequest{
DeviceID: uint64(d.Get("device_id").(int)),
}
_, err := c.CloudBroker().PCIDevice().Enable(ctx, req)
if err != nil {
return diag.FromErr(err)
}
} else {
req := pcidevice.DisableRequest{
DeviceID: uint64(d.Get("device_id").(int)),
}
if force, ok := d.GetOk("force"); ok {
req.Force = force.(bool)
}
_, err := c.CloudBroker().PCIDevice().Disable(ctx, req)
if err != nil {
return diag.FromErr(err)
}
}
}
return resourcePcideviceRead(ctx, d, m)
}
func resourcePcideviceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceDelete: called for %s, id: %s", d.Get("name").(string), d.Id())
pciDevice, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
if pciDevice.Status == status.Destroyed || pciDevice.Status == status.Purged {
return nil
}
c := m.(*controller.ControllerCfg)
req := pcidevice.DeleteRequest{
DeviceID: pciDevice.ID,
}
if force, ok := d.GetOk("force"); ok {
req.Force = force.(bool)
}
_, err = c.CloudBroker().PCIDevice().Delete(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func resourcePcideviceSchemaMake() map[string]*schema.Schema {
return map[string]*schema.Schema{
"ckey": {
Type: schema.TypeString,
Computed: true,
},
"meta": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"compute_id": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "description, just for information",
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"hw_path": {
Type: schema.TypeString,
Required: true,
Description: "PCI address of the device",
},
"device_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of Device",
},
"rg_id": {
Type: schema.TypeInt,
Required: true,
Description: "Resource GROUP",
},
"stack_id": {
Type: schema.TypeInt,
Required: true,
Description: "stackId",
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"system_name": {
Type: schema.TypeString,
Computed: true,
},
"force": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Force delete",
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable pci device",
},
}
}
func ResourcePcidevice() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
CreateContext: resourcePcideviceCreate,
ReadContext: resourcePcideviceRead,
UpdateContext: resourcePcideviceUpdate,
DeleteContext: resourcePcideviceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: &constants.Timeout60s,
Read: &constants.Timeout30s,
Update: &constants.Timeout60s,
Delete: &constants.Timeout60s,
Default: &constants.Timeout60s,
},
Schema: resourcePcideviceSchemaMake(),
}
}
/*
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package pcidevice
import (
"context"
log "github.com/sirupsen/logrus"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/pcidevice"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/status"
)
func resourcePcideviceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceCreate: called for pcidevice %s", d.Get("name").(string))
c := m.(*controller.ControllerCfg)
createReq := pcidevice.CreateRequest{}
if diags := checkParamsExistence(ctx, d, c); diags != nil {
return diags
}
createReq.StackID = uint64(d.Get("stack_id").(int))
createReq.RGID = uint64(d.Get("rg_id").(int))
createReq.Name = d.Get("name").(string)
createReq.HWPath = d.Get("hw_path").(string)
if description, ok := d.GetOk("description"); ok {
createReq.Description = description.(string)
}
pcideviceId, err := c.CloudBroker().PCIDevice().Create(ctx, createReq)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
d.SetId(strconv.FormatUint(pcideviceId, 10))
d.Set("device_id", pcideviceId)
warnings := dc.Warnings{}
if enable, ok := d.GetOk("enable"); ok {
log.Debugf("resourcePcideviceCreate: enable=%t device_id %d after completing its resource configuration", enable, pcideviceId)
if enable.(bool) {
req := pcidevice.EnableRequest{DeviceID: pcideviceId}
if _, err := c.CloudBroker().PCIDevice().Enable(ctx, req); err != nil {
warnings.Add(err)
}
} else {
req := pcidevice.DisableRequest{
DeviceID: pcideviceId,
}
if force, ok := d.GetOk("force_disable"); ok {
req.Force = force.(bool)
log.Debugf("force_disable=%v", force)
}
if _, err := c.CloudBroker().PCIDevice().Disable(ctx, req); err != nil {
warnings.Add(err)
}
}
}
return append(resourcePcideviceRead(ctx, d, m), warnings.Get()...)
}
func resourcePcideviceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceRead: called for pci_device id %d, name %s",
d.Id(), d.Get("name").(string))
pcideviceRec, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
flattenPcidevice(d, pcideviceRec)
log.Debugf("resourcePcideviceRead: after flattenPcidevice: device_id %s, name %s",
d.Id(), d.Get("name").(string))
return nil
}
func resourcePcideviceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceUpdate: called for pcidevice id %s, name %s", d.Id(), d.Get("name").(string))
c := m.(*controller.ControllerCfg)
if diags := checkParamsExistence(ctx, d, c); diags != nil {
return diags
}
if d.HasChange("enable") {
err := resourcePcideviceChangeEnable(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
return resourcePcideviceRead(ctx, d, m)
}
func resourcePcideviceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourcePcideviceDelete: called for %s, id: %s", d.Get("name").(string), d.Id())
c := m.(*controller.ControllerCfg)
pciDevice, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
if pciDevice.Status == status.Destroyed || pciDevice.Status == status.Purged {
return nil
}
req := pcidevice.DeleteRequest{
DeviceID: pciDevice.ID,
}
if force, ok := d.GetOk("force_delete"); ok {
req.Force = force.(bool)
}
if _, err = c.CloudBroker().PCIDevice().Delete(ctx, req); err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func ResourcePcidevice() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
CreateContext: resourcePcideviceCreate,
ReadContext: resourcePcideviceRead,
UpdateContext: resourcePcideviceUpdate,
DeleteContext: resourcePcideviceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: &constants.Timeout60s,
Read: &constants.Timeout30s,
Update: &constants.Timeout60s,
Delete: &constants.Timeout60s,
Default: &constants.Timeout60s,
},
Schema: resourcePcideviceSchemaMake(),
}
}
func resourcePcideviceChangeEnable(ctx context.Context, d *schema.ResourceData, m interface{}) error {
enable := d.Get("enable").(bool)
log.Debugf("resourcePcideviceChangeEnable: enable=%t device_id %s after completing its resource configuration", enable, d.Id())
pcideviceRec, err := utilityPcideviceCheckPresence(ctx, d, m)
if err != nil {
return err
}
c := m.(*controller.ControllerCfg)
if enable {
req := pcidevice.EnableRequest{
DeviceID: pcideviceRec.ID,
}
if _, err := c.CloudBroker().PCIDevice().Enable(ctx, req); err != nil {
return err
}
} else {
req := pcidevice.DisableRequest{
DeviceID: pcideviceRec.ID,
}
if force, ok := d.GetOk("force_disable"); ok {
req.Force = force.(bool)
}
if _, err := c.CloudBroker().PCIDevice().Disable(ctx, req); err != nil {
return err
}
}
return nil
}