|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + "github.com/netapp/ontap-mcp/ontap" |
| 10 | + "github.com/netapp/ontap-mcp/tool" |
| 11 | +) |
| 12 | + |
| 13 | +func (a *App) CreateLunMap(ctx context.Context, _ *mcp.CallToolRequest, parameters tool.LunMap) (*mcp.CallToolResult, any, error) { |
| 14 | + if !a.locks.TryLock(parameters.Cluster) { |
| 15 | + return errorResult(fmt.Errorf("another write operation is in progress on cluster %s, please try again", parameters.Cluster)), nil, nil |
| 16 | + } |
| 17 | + defer a.locks.Unlock(parameters.Cluster) |
| 18 | + |
| 19 | + lunMapCreate, err := newCreateLunMap(parameters) |
| 20 | + if err != nil { |
| 21 | + return nil, nil, err |
| 22 | + } |
| 23 | + |
| 24 | + client, err := a.getClient(parameters.Cluster) |
| 25 | + if err != nil { |
| 26 | + return errorResult(err), nil, err |
| 27 | + } |
| 28 | + |
| 29 | + err = client.CreateLunMap(ctx, lunMapCreate) |
| 30 | + if err != nil { |
| 31 | + return errorResult(err), nil, err |
| 32 | + } |
| 33 | + |
| 34 | + return &mcp.CallToolResult{ |
| 35 | + Content: []mcp.Content{ |
| 36 | + &mcp.TextContent{Text: "lun map created successfully"}, |
| 37 | + }, |
| 38 | + }, nil, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (a *App) DeleteLunMap(ctx context.Context, _ *mcp.CallToolRequest, parameters tool.LunMap) (*mcp.CallToolResult, any, error) { |
| 42 | + if !a.locks.TryLock(parameters.Cluster) { |
| 43 | + return errorResult(fmt.Errorf("another write operation is in progress on cluster %s, please try again", parameters.Cluster)), nil, nil |
| 44 | + } |
| 45 | + defer a.locks.Unlock(parameters.Cluster) |
| 46 | + |
| 47 | + if err := validateDeleteLunMap(parameters); err != nil { |
| 48 | + return nil, nil, err |
| 49 | + } |
| 50 | + |
| 51 | + client, err := a.getClient(parameters.Cluster) |
| 52 | + if err != nil { |
| 53 | + return errorResult(err), nil, err |
| 54 | + } |
| 55 | + |
| 56 | + err = client.DeleteLunMap(ctx, parameters.SVM, parameters.LunName, parameters.IGroupName) |
| 57 | + if err != nil { |
| 58 | + return errorResult(err), nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return &mcp.CallToolResult{ |
| 62 | + Content: []mcp.Content{ |
| 63 | + &mcp.TextContent{Text: "lun map deleted successfully"}, |
| 64 | + }, |
| 65 | + }, nil, nil |
| 66 | +} |
| 67 | + |
| 68 | +func newCreateLunMap(in tool.LunMap) (ontap.LunMap, error) { |
| 69 | + out := ontap.LunMap{} |
| 70 | + if in.SVM == "" { |
| 71 | + return out, errors.New("SVM name is required") |
| 72 | + } |
| 73 | + if in.LunName == "" { |
| 74 | + return out, errors.New("LUN name is required") |
| 75 | + } |
| 76 | + if in.IGroupName == "" { |
| 77 | + return out, errors.New("igroup name is required") |
| 78 | + } |
| 79 | + |
| 80 | + out.SVM = ontap.NameAndUUID{Name: in.SVM} |
| 81 | + out.Lun = ontap.NameAndUUID{Name: in.LunName} |
| 82 | + out.IGroup = ontap.NameAndUUID{Name: in.IGroupName} |
| 83 | + return out, nil |
| 84 | +} |
| 85 | + |
| 86 | +func validateDeleteLunMap(in tool.LunMap) error { |
| 87 | + if in.SVM == "" { |
| 88 | + return errors.New("SVM name is required") |
| 89 | + } |
| 90 | + if in.LunName == "" { |
| 91 | + return errors.New("LUN name is required") |
| 92 | + } |
| 93 | + if in.IGroupName == "" { |
| 94 | + return errors.New("igroup name is required") |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
0 commit comments