Skip to content

Commit

Permalink
feat(BM-Reservations-Automatic-Attachment): Baremetal Server Reservat…
Browse files Browse the repository at this point in the history
…ions and Reservations Automatic Attachment (#5805)

* feat(BM-Reservations-Automatic-Attachment): Baremetal Server Reservations and Reservations Automatic Attachment

* update vpc-go-sdk
  • Loading branch information
sreekarbvibm authored Nov 24, 2024
1 parent 201986b commit 7f6c0e3
Show file tree
Hide file tree
Showing 22 changed files with 1,208 additions and 52 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.7
github.com/IBM/vmware-go-sdk v0.1.2
github.com/IBM/vpc-beta-go-sdk v0.8.0
github.com/IBM/vpc-go-sdk v0.62.0
github.com/IBM/vpc-go-sdk v0.63.1
github.com/ScaleFT/sshkeys v0.0.0-20200327173127-6142f742bca5
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2
github.com/akamai/AkamaiOPEN-edgegrid-golang/v5 v5.0.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ github.com/IBM/vpc-beta-go-sdk v0.8.0 h1:cEPpv4iw3Ba5W2d0AWg3TIbKeJ8y1nPuUuibR5J
github.com/IBM/vpc-beta-go-sdk v0.8.0/go.mod h1:hORgIyTFRzXrZIK9IohaWmCRBBlYiDRagsufi7M6akE=
github.com/IBM/vpc-go-sdk v0.62.0 h1:Xga74D70ziD7nzm51ue3othHz1epMLVkGP/L6/Be+/0=
github.com/IBM/vpc-go-sdk v0.62.0/go.mod h1:VBR6bAznHsNCFA89Ue4JFQpqCcFp8F5neqbCFCyks4Q=
github.com/IBM/vpc-go-sdk v0.63.0 h1:0eSG/8WLK4fW7AvPHlhCmPEe819pgWLF1OIxF0vR43A=
github.com/IBM/vpc-go-sdk v0.63.0/go.mod h1:VBR6bAznHsNCFA89Ue4JFQpqCcFp8F5neqbCFCyks4Q=
github.com/IBM/vpc-go-sdk v0.63.1 h1:HqQeq2wGI2pF4y0/m18EaPsOEEXFjyml+xwlLC9AiXE=
github.com/IBM/vpc-go-sdk v0.63.1/go.mod h1:VBR6bAznHsNCFA89Ue4JFQpqCcFp8F5neqbCFCyks4Q=
github.com/Jeffail/gabs v1.1.1 h1:V0uzR08Hj22EX8+8QMhyI9sX2hwRu+/RJhJUmnwda/E=
github.com/Jeffail/gabs v1.1.1/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56 h1:vuquMR410psHNax14XKNWa0Ae/kYgWJcXi0IFuX60N0=
Expand Down
203 changes: 203 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_bare_metal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,144 @@ func DataSourceIBMIsBareMetalServer() *schema.Resource {
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
"health_reasons": {
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current health_state (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the reason for this health state.",
},
"message": {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the reason for this health state.",
},
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about the reason for this health state.",
},
},
},
},
"health_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The health of this resource",
},
isReservation: {
Type: schema.TypeList,
Computed: true,
Description: "The reservation used by this bare metal server",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isReservationId: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reservation.",
},
isReservationCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this reservation.",
},
isReservationName: {
Type: schema.TypeString,
Computed: true,
Description: "The name for this reservation. The name is unique across all reservations in the region.",
},
isReservationHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reservation.",
},
isReservationResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isReservationDeleted: &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isReservationDeletedMoreInfo: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
},
},
},
isReservationAffinity: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isReservationAffinityPolicyResp: {
Type: schema.TypeString,
Computed: true,
Description: "The reservation affinity policy to use for this bare metal server.",
},
isReservationAffinityPool: &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The pool of reservations available for use by this bare metal server.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isReservationId: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reservation.",
},
isReservationCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this reservation.",
},
isReservationName: {
Type: schema.TypeString,
Computed: true,
Description: "The name for this reservation. The name is unique across all reservations in the region.",
},
isReservationHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reservation.",
},
isReservationResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isReservationDeleted: &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isReservationDeletedMoreInfo: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
},
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -1161,6 +1299,71 @@ func dataSourceIBMISBareMetalServerRead(context context.Context, d *schema.Resou
}
d.Set(isBareMetalServerAccessTags, accesstags)

if bms.HealthReasons != nil {
healthReasonsList := make([]map[string]interface{}, 0)
for _, sr := range bms.HealthReasons {
currentSR := map[string]interface{}{}
if sr.Code != nil && sr.Message != nil {
currentSR["code"] = *sr.Code
currentSR["message"] = *sr.Message
if sr.MoreInfo != nil {
currentSR["more_info"] = *sr.Message
}
healthReasonsList = append(healthReasonsList, currentSR)
}
}
d.Set("health_reasons", healthReasonsList)
}
if err = d.Set("health_state", bms.HealthState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting health_state: %s", err))
}
if bms.ReservationAffinity != nil {
reservationAffinity := []map[string]interface{}{}
reservationAffinityMap := map[string]interface{}{}

reservationAffinityMap[isReservationAffinityPolicyResp] = bms.ReservationAffinity.Policy
if bms.ReservationAffinity.Pool != nil {
poolList := make([]map[string]interface{}, 0)
for _, pool := range bms.ReservationAffinity.Pool {
res := map[string]interface{}{}

res[isReservationId] = *pool.ID
res[isReservationHref] = *pool.Href
res[isReservationName] = *pool.Name
res[isReservationCrn] = *pool.CRN
res[isReservationResourceType] = *pool.ResourceType
if pool.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceReservationDeletedToMap(*pool.Deleted)
deletedList = append(deletedList, deletedMap)
res[isReservationDeleted] = deletedList
}
poolList = append(poolList, res)
}
reservationAffinityMap[isReservationAffinityPool] = poolList
}
reservationAffinity = append(reservationAffinity, reservationAffinityMap)
d.Set(isReservationAffinity, reservationAffinity)
}
if bms.Reservation != nil {
resList := make([]map[string]interface{}, 0)
res := map[string]interface{}{}

res[isReservationId] = *bms.Reservation.ID
res[isReservationHref] = *bms.Reservation.Href
res[isReservationName] = *bms.Reservation.Name
res[isReservationCrn] = *bms.Reservation.CRN
res[isReservationResourceType] = *bms.Reservation.ResourceType
if bms.Reservation.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceReservationDeletedToMap(*bms.Reservation.Deleted)
deletedList = append(deletedList, deletedMap)
res[isReservationDeleted] = deletedList
}
resList = append(resList, res)
d.Set(isReservation, resList)
}

return nil
}

Expand Down
49 changes: 49 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_bare_metal_server_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,28 @@ func DataSourceIBMIsBareMetalServerProfile() *schema.Resource {
},
},
},
"reservation_terms": {
Type: schema.TypeList,
Computed: true,
Description: "The type for this profile field",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type for this profile field.",
},
"values": {
Type: schema.TypeList,
Computed: true,
Description: "The supported committed use terms for a reservation using this profile",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -611,10 +633,37 @@ func dataSourceIBMISBMSProfileRead(context context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf("Error setting network_attachment_count %s", err))
}
}
if bmsProfile.ReservationTerms != nil {
err = d.Set("reservation_terms", dataSourceBaremetalServerProfileFlattenReservationTerms(*bmsProfile.ReservationTerms))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting reservation_terms %s", err))
}
}

return nil
}

func dataSourceBaremetalServerProfileFlattenReservationTerms(result vpcv1.BareMetalServerProfileReservationTerms) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceBaremetalServerProfileReservationTermsToMap(result)
finalList = append(finalList, finalMap)

return finalList
}

func dataSourceBaremetalServerProfileReservationTermsToMap(resTermItem vpcv1.BareMetalServerProfileReservationTerms) map[string]interface{} {
resTermMap := map[string]interface{}{}

if resTermItem.Type != nil {
resTermMap["type"] = resTermItem.Type
}
if resTermItem.Values != nil {
resTermMap["values"] = resTermItem.Values
}

return resTermMap
}

func dataSourceIBMIsBareMetalServerProfileBareMetalServerProfileConsoleTypesToMap(model *vpcv1.BareMetalServerProfileConsoleTypes) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["type"] = model.Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestAccIBMISBMSProfileDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resName, "os_architecture.#"),
resource.TestCheckResourceAttrSet(resName, "resource_type"),
resource.TestCheckResourceAttrSet(resName, "supported_trusted_platform_module_modes.#"),
resource.TestCheckResourceAttrSet(resName, "reservation_terms.#"),
resource.TestCheckResourceAttrSet(resName, "reservation_terms.0.type"),
resource.TestCheckResourceAttrSet(resName, "reservation_terms.0.values"),
),
},
},
Expand Down
Loading

0 comments on commit 7f6c0e3

Please sign in to comment.