-
Notifications
You must be signed in to change notification settings - Fork 6
feat: adding lun crud tools with tests #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d0b62d
feat: adding lun crud tools with tests
Hardikl 8108dc4
Merge remote-tracking branch 'origin/main' into hl_lun_tool
Hardikl 6c9f432
feat: update test cases
Hardikl f74ec21
Merge remote-tracking branch 'origin/main' into hl_lun_tool
Hardikl 46d011b
feat: handled review comment
Hardikl 6b4cdad
Merge remote-tracking branch 'origin/main' into hl_lun_tool
Hardikl d14878f
feat: handled review comments
Hardikl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "log/slog" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/netapp/ontap-mcp/config" | ||
| ) | ||
|
|
||
| func TestLUN(t *testing.T) { | ||
| SkipIfMissing(t, CheckTools) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expectedOntapErr string | ||
| verifyAPI ontapVerifier | ||
| }{ | ||
| { | ||
| name: "Clean SVM", | ||
| input: ClusterStr + "delete " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/svm/svms?name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
| { | ||
| name: "Create SVM", | ||
| input: ClusterStr + "create " + rn("marketing") + " svm", | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{api: "api/svm/svms?name=" + rn("marketing"), validationFunc: createObject}, | ||
| }, | ||
| { | ||
| name: "Clean LUN", | ||
| input: ClusterStr + "delete lun " + rn("lundoc") + " in volume " + rn("doc") + " in " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/storage/luns?name=/vol/" + rn("doc") + "/" + rn("lundoc") + "&svm.name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
| { | ||
| name: "Clean LUN", | ||
| input: ClusterStr + "delete lun " + rn("lundocnew") + " in volume " + rn("doc") + " in " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/storage/luns?name=/vol/" + rn("doc") + "/" + rn("lundocnew") + "&svm.name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
|
Hardikl marked this conversation as resolved.
|
||
| { | ||
| name: "Create volume", | ||
| input: ClusterStr + "create a 100MB volume named " + rn("doc") + " on the " + rn("marketing") + " svm and the harvest_vc_aggr aggregate", | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{api: "api/storage/volumes?name=" + rn("doc") + "&svm.name=" + rn("marketing"), validationFunc: createObject}, | ||
| }, | ||
| { | ||
| name: "Create LUN", | ||
| input: ClusterStr + "create a 20MB lun named " + rn("lundoc") + " in volume " + rn("doc") + " on the " + rn("marketing") + " svm with os type linux", | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{api: "api/storage/luns?name=/vol/" + rn("doc") + "/" + rn("lundoc") + "&svm.name=" + rn("marketing"), validationFunc: createObject}, | ||
| }, | ||
| { | ||
| name: "Update lun size", | ||
| input: ClusterStr + "update lun " + rn("lundoc") + " size to 50mb in volume " + rn("doc") + " on the " + rn("marketing") + " svm", | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{}, | ||
| }, | ||
| { | ||
| name: "Rename lun", | ||
| input: ClusterStr + "rename the lun " + rn("lundoc") + " in volume " + rn("doc") + " on the " + rn("marketing") + " svm to " + rn("lundocnew"), | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{api: "api/storage/luns?name=/vol/" + rn("doc") + "/" + rn("lundocnew") + "&svm.name=" + rn("marketing"), validationFunc: createObject}, | ||
| }, | ||
| { | ||
| name: "Update lun state", | ||
| input: ClusterStr + "disable the lun " + rn("lundocnew") + " in volume " + rn("doc") + " on the " + rn("marketing") + " svm", | ||
| expectedOntapErr: "", | ||
| verifyAPI: ontapVerifier{}, | ||
| }, | ||
| { | ||
| name: "Clean LUN", | ||
| input: ClusterStr + "delete lun " + rn("lundocnew") + " in volume " + rn("doc") + " in " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/storage/luns?name=/vol/" + rn("doc") + "/" + rn("lundocnew") + "&svm.name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
| { | ||
| name: "Clean volume", | ||
| input: ClusterStr + "delete volume " + rn("doc") + " in " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/storage/volumes?name=" + rn("doc") + "&svm.name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
| { | ||
| name: "Clean SVM", | ||
| input: ClusterStr + "delete " + rn("marketing") + " svm", | ||
| expectedOntapErr: "because it does not exist", | ||
| verifyAPI: ontapVerifier{api: "api/svm/svms?name=" + rn("marketing"), validationFunc: deleteObject}, | ||
| }, | ||
| } | ||
|
|
||
| cfg, err := config.ReadConfig(ConfigFile) | ||
| if err != nil { | ||
| t.Fatalf("Error parsing the config: %v", err) | ||
| } | ||
|
|
||
| poller := cfg.Pollers[Cluster] | ||
| transport := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{ | ||
| InsecureSkipVerify: poller.UseInsecureTLS, // #nosec G402 | ||
| }, | ||
| } | ||
| client := &http.Client{Transport: transport, Timeout: 10 * time.Second} | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| slog.Debug("", slog.String("Input", tt.input)) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) | ||
| defer cancel() | ||
| if _, err := testAgent.ChatWithResponse(ctx, t, tt.input, tt.expectedOntapErr); err != nil { | ||
| t.Fatalf("Error processing input %q: %v", tt.input, err) | ||
| } | ||
| if tt.verifyAPI.api != "" && !tt.verifyAPI.validationFunc(t, tt.verifyAPI.api, poller, client) { | ||
| t.Errorf("Error while accessing the object via prompt %q", tt.input) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package rest | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "github.com/netapp/ontap-mcp/ontap" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| ) | ||
|
|
||
| func (c *Client) CreateLUN(ctx context.Context, lun ontap.LUN) error { | ||
| var ( | ||
| statusCode int | ||
| ) | ||
| responseHeaders := http.Header{} | ||
|
|
||
| builder := c.baseRequestBuilder(`/api/storage/luns`, &statusCode, responseHeaders). | ||
| BodyJSON(lun) | ||
|
|
||
| if err := c.buildAndExecuteRequest(ctx, builder); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.checkStatus(statusCode) | ||
| } | ||
|
|
||
| func (c *Client) UpdateLUN(ctx context.Context, svmName, lunPath string, lun ontap.LUN) error { | ||
| var ( | ||
| statusCode int | ||
| lunData ontap.GetData | ||
| ) | ||
| responseHeaders := http.Header{} | ||
|
|
||
| params := url.Values{} | ||
| params.Set("name", lunPath) | ||
| params.Set("svm.name", svmName) | ||
| params.Set("fields", "uuid") | ||
|
|
||
| builder := c.baseRequestBuilder(`/api/storage/luns`, &statusCode, responseHeaders). | ||
| Params(params). | ||
| ToJSON(&lunData) | ||
|
|
||
| if err := c.buildAndExecuteRequest(ctx, builder); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if lunData.NumRecords == 0 { | ||
| return fmt.Errorf("failed to get lun=%s on svm=%s because it does not exist", lunPath, svmName) | ||
| } | ||
| if lunData.NumRecords != 1 { | ||
| return fmt.Errorf("failed to get lun=%s on svm=%s because there are %d matching records", lunPath, svmName, lunData.NumRecords) | ||
| } | ||
|
|
||
| builder2 := c.baseRequestBuilder(`/api/storage/luns/`+lunData.Records[0].UUID, &statusCode, responseHeaders). | ||
| Patch(). | ||
| BodyJSON(lun) | ||
|
|
||
| if err := c.buildAndExecuteRequest(ctx, builder2); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.checkStatus(statusCode) | ||
| } | ||
|
|
||
| func (c *Client) DeleteLUN(ctx context.Context, svmName, lunPath string, allowDeleteWhileMapped bool) error { | ||
| var ( | ||
| statusCode int | ||
| lunData ontap.GetData | ||
| ) | ||
| responseHeaders := http.Header{} | ||
|
|
||
| params := url.Values{} | ||
| params.Set("name", lunPath) | ||
| params.Set("svm.name", svmName) | ||
| params.Set("fields", "uuid") | ||
|
|
||
| builder := c.baseRequestBuilder(`/api/storage/luns`, &statusCode, responseHeaders). | ||
| Params(params). | ||
| ToJSON(&lunData) | ||
|
|
||
| if err := c.buildAndExecuteRequest(ctx, builder); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if lunData.NumRecords == 0 { | ||
| return fmt.Errorf("failed to get lun=%s on svm=%s because it does not exist", lunPath, svmName) | ||
| } | ||
| if lunData.NumRecords != 1 { | ||
| return fmt.Errorf("failed to get lun=%s on svm=%s because there are %d matching records", lunPath, svmName, lunData.NumRecords) | ||
| } | ||
|
|
||
| deleteParams := url.Values{} | ||
| deleteParams.Set("allow_delete_while_mapped", strconv.FormatBool(allowDeleteWhileMapped)) | ||
| builder2 := c.baseRequestBuilder(`/api/storage/luns/`+lunData.Records[0].UUID, &statusCode, responseHeaders). | ||
| Params(deleteParams). | ||
| Delete() | ||
|
|
||
| if err := c.buildAndExecuteRequest(ctx, builder2); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.checkStatus(statusCode) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.