This repository was archived by the owner on Jun 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathibpartition.go
More file actions
192 lines (162 loc) · 7 KB
/
Copy pathibpartition.go
File metadata and controls
192 lines (162 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
package activity
import (
"context"
"errors"
"time"
swe "github.com/NVIDIA/ncx-infra-controller-rest/site-workflow/pkg/error"
"github.com/NVIDIA/ncx-infra-controller-rest/site-workflow/pkg/grpc/client"
cClient "github.com/NVIDIA/ncx-infra-controller-rest/site-workflow/pkg/grpc/client"
cwssaws "github.com/NVIDIA/ncx-infra-controller-rest/workflow-schema/schema/site-agent/workflows/v1"
"github.com/rs/zerolog/log"
"go.temporal.io/sdk/temporal"
"google.golang.org/protobuf/types/known/timestamppb"
)
// ManageInfiniBandPartitionInventory is an activity wrapper for InfiniBand Partition inventory collection and publishing
type ManageInfiniBandPartitionInventory struct {
config ManageInventoryConfig
}
// DiscoverInfiniBandPartitionInventory is an activity to collect InfiniBand Partition inventory and publish to Temporal queue
func (mmi *ManageInfiniBandPartitionInventory) DiscoverInfiniBandPartitionInventory(ctx context.Context) error {
logger := log.With().Str("Activity", "DiscoverIBPartitionInventory").Logger()
logger.Info().Msg("Starting activity")
inventoryImpl := manageInventoryImpl[*cwssaws.IBPartitionId, *cwssaws.IBPartition, *cwssaws.InfiniBandPartitionInventory]{
itemType: "InfiniBandPartition",
config: mmi.config,
internalFindIDs: ibpFindIDs,
internalFindByIDs: ibpFindByIDs,
internalPagedInventory: ibpPagedInventory,
}
return inventoryImpl.CollectAndPublishInventory(ctx, &logger)
}
// NewManageInfiniBandPartitionInventory returns a ManageInventory implementation for InfiniBand Partition activity
func NewManageInfiniBandPartitionInventory(config ManageInventoryConfig) ManageInfiniBandPartitionInventory {
return ManageInfiniBandPartitionInventory{
config: config,
}
}
func ibpFindIDs(ctx context.Context, carbideClient *cClient.CarbideClient) ([]*cwssaws.IBPartitionId, error) {
idList, err := carbideClient.Networks().FindInfinibandPartitionIDs(ctx, &cwssaws.IBPartitionSearchFilter{})
if err != nil {
return nil, err
}
return idList.GetIbPartitionIds(), nil
}
func ibpFindByIDs(ctx context.Context, carbideClient *cClient.CarbideClient, ids []*cwssaws.IBPartitionId) ([]*cwssaws.IBPartition, error) {
list, err := carbideClient.Networks().FindInfinibandPartitionsByIDs(ctx, &cwssaws.IBPartitionsByIdsRequest{
IbPartitionIds: ids,
})
if err != nil {
return nil, err
}
return list.GetIbPartitions(), nil
}
func ibpPagedInventory(allItemIDs []*cwssaws.IBPartitionId, pagedItems []*cwssaws.IBPartition, input *pagedInventoryInput) *cwssaws.InfiniBandPartitionInventory {
itemIDs := []string{}
for _, id := range allItemIDs {
itemIDs = append(itemIDs, id.GetValue())
}
// Create an inventory page with the subset of VPCs
inventory := &cwssaws.InfiniBandPartitionInventory{
IbPartitions: pagedItems,
Timestamp: ×tamppb.Timestamp{
Seconds: time.Now().Unix(),
},
InventoryStatus: input.status,
StatusMsg: input.statusMessage,
InventoryPage: input.buildPage(),
}
if inventory.InventoryPage != nil {
inventory.InventoryPage.ItemIds = itemIDs
}
return inventory
}
// ManageInfiniBandPartition is an activity wrapper for InfiniBand Partition management
type ManageInfiniBandPartition struct {
CarbideAtomicClient *client.CarbideAtomicClient
}
// NewManageInfiniBandPartition returns a new ManageInfiniBandPartition client
func NewManageInfiniBandPartition(carbideClient *client.CarbideAtomicClient) ManageInfiniBandPartition {
return ManageInfiniBandPartition{
CarbideAtomicClient: carbideClient,
}
}
// Function to create InfiniBand Partition with Carbide
func (mibp *ManageInfiniBandPartition) CreateInfiniBandPartitionOnSite(ctx context.Context, request *cwssaws.IBPartitionCreationRequest) error {
logger := log.With().Str("Activity", "CreateInfiniBandPartitionOnSite").Logger()
logger.Info().Msg("Starting activity")
var err error
// Validate request
if request == nil {
err = errors.New("received empty create InfiniBand Partition request")
} else if request.Id == nil || request.GetId().GetValue() == "" {
err = errors.New("received create InfiniBand Partition request without ID")
} else if request.GetConfig() == nil {
err = errors.New("received create InfiniBand Partition request without Config")
} else if request.GetMetadata().GetName() == "" && request.GetConfig().GetName() == "" {
// Backward compatibility: both Metadata.Name and Config.Name are accepted
err = errors.New("received create InfiniBand Partition request without Name")
} else if request.GetConfig().GetTenantOrganizationId() == "" {
err = errors.New("received create InfiniBand Partition request without TenantOrganizationId")
}
if err != nil {
return temporal.NewNonRetryableApplicationError(err.Error(), swe.ErrTypeInvalidRequest, err)
}
// Call Site Controller gRPC endpoint
carbideClient := mibp.CarbideAtomicClient.GetClient()
if carbideClient == nil {
return client.ErrClientNotConnected
}
forgeClient := carbideClient.Carbide()
// Call Forge gRPC endpoint
_, err = forgeClient.CreateIBPartition(ctx, request)
if err != nil {
logger.Warn().Err(err).Msg("Failed to create InfiniBand Partition using Site Controller API")
return swe.WrapErr(err)
}
logger.Info().Msg("Completed activity")
return nil
}
// Function to delete InfiniBand Partition on Carbide
func (mipb *ManageInfiniBandPartition) DeleteInfiniBandPartitionOnSite(ctx context.Context, request *cwssaws.IBPartitionDeletionRequest) error {
logger := log.With().Str("Activity", "DeleteInfiniBandPartitionOnSite").Logger()
logger.Info().Msg("Starting activity")
var err error
// Validate request
if request == nil {
err = errors.New("received empty delete InfiniBand Partition request")
} else if request.Id == nil || request.Id.GetValue() == "" {
err = errors.New("received delete InfiniBand Partition request without ID")
}
if err != nil {
return temporal.NewNonRetryableApplicationError(err.Error(), swe.ErrTypeInvalidRequest, err)
}
// Call Site Controller gRPC endpoint
carbideClient := mipb.CarbideAtomicClient.GetClient()
if carbideClient == nil {
return client.ErrClientNotConnected
}
forgeClient := carbideClient.Carbide()
_, err = forgeClient.DeleteIBPartition(ctx, request)
if err != nil {
logger.Warn().Err(err).Msg("Failed to delete InfiniBand Partition using Site Controller API")
return swe.WrapErr(err)
}
logger.Info().Msg("Completed activity")
return nil
}