Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/server_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ func main() {
fmt.Println("#----------------Server Profile Created---------------#")
}

// Example: Submit a new Server Profile asynchronously
//
// This example demonstrates how to use the `SubmitNewProfileAsync` function to create
// a new server profile in OneView. The call is asynchronous, meaning it immediately
// returns a Task object instead of waiting for the operation to finish.
//
// The returned Task contains the URI that can be polled or monitored until the
// profile creation completes. If the server hardware already has a profile assigned
// or if the hardware is powered on, an error will be returned.
task, err := ovc.SubmitNewProfileAsync(server_profile_create_map, ignoreFlags...)
if err != nil {
fmt.Println("Server Profile Create Failed: ", err)
} else {

if err != nil {
fmt.Println("Error marshaling task:", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some documentation regarding the new async server profile creation function. In the next release add it in the release notes also

} else {
fmt.Println("Task URI:")
fmt.Println(string(task.URI))
}
fmt.Println("#----------------Server Profile Creation Task Initiated---------------#")
}

sort := ""
server_name := ov.ServerHardwareType{}
spt, err := ovc.GetProfileTemplateByName(spt_name)
Expand Down
80 changes: 80 additions & 0 deletions ov/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,86 @@ func (c *OVClient) SubmitNewProfile(p ServerProfile, ignoreFlags ...ForceFlag) (
return nil
}

// SubmitNewProfileAsync - submit new profile template
func (c *OVClient) SubmitNewProfileAsync(
p ServerProfile,
ignoreFlags ...ForceFlag,
) (*Task, error) {
var err error
log.Infof("Initializing creation of server profile for %s.", p.Name)
var (
uri = "/rest/server-profiles"
server ServerHardware
t *Task
// if no warning flags has been provided, use default value:
forceFlags = map[string]interface{}{
"force": ForceIgnoreNone,
}
)
// refresh login
c.RefreshLogin()
c.SetAuthHeaderOptions(c.GetAuthHeaderMap())

t = t.NewProfileTask(c)
t.ResetTask()
log.Debugf("REST : %s \n %+v\n", uri, p)
log.Debugf("task -> %+v", t)

// Check if server hardware already has server profile assigned
if server.ServerProfileURI.String() != "null" {
return nil, fmt.Errorf("hardware %s already has server profile assigned", server.Name)
}

server, err = c.GetServerHardwareByUri(p.ServerHardwareURI)

if err != nil {
log.Warnf("Problem getting server hardware, %s", err)
}

// power off the server so that we can add to SP
if server.Name != "" && server.PowerState == "on" {
return nil, errors.New("Server Hardware must be powered off to assign to the server profile")
}

serverHardwareType, err := c.GetServerHardwareTypeByUri(server.ServerHardwareTypeURI)
if err != nil {
log.Warnf("Error getting server hardware type %s", err)
}
serverHarwdareTypeGen := serverHardwareType.Generation

var emptyMgmtProcessorsStruct ManagementProcessors
if !reflect.DeepEqual(p.ManagementProcessors, emptyMgmtProcessorsStruct) {
mp := SetMp(serverHarwdareTypeGen, p.ManagementProcessors)
p.ManagementProcessor = mp
}

// append force flags comma separated
if len(ignoreFlags) > 0 {
var flags []string

for _, i := range ignoreFlags {
flags = append(flags, i.String())
}

forceFlags["force"] = strings.Join(flags, ",")
}

data, err := c.RestAPICall(rest.POST, uri, p, forceFlags)
if err != nil {
t.TaskIsDone = true
log.Errorf("Error submitting new profile request: %s", err)
return nil, err
}

log.Debugf("Response New Profile %s", data)
if err := json.Unmarshal([]byte(data), &t); err != nil {
t.TaskIsDone = true
log.Errorf("Error with task un-marshal: %s", err)
return nil, err
}
return t, nil
}

// create profile from template
func (c *OVClient) CreateProfileFromTemplate(name string, template ServerProfile, blade ServerHardware, ignoreFlags ...ForceFlag) error {
log.Debugf("TEMPLATE : %+v\n", template)
Expand Down