-
Notifications
You must be signed in to change notification settings - Fork 12
Add tag support for IPAM/Prefix resources #415
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
f26127e
7bf39e9
2a226da
740bf8a
e8cca22
0c17645
0cf1acb
20c7d03
a342718
b80a13d
c79715c
c2adfad
fddee69
ee721f3
12fb706
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| Copyright 2024 Swisscom (Schweiz) AG. | ||
|
|
||
| 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 v1 | ||
|
|
||
| type Tag struct { | ||
| // +optional | ||
| // Name of the tag | ||
| Name string `json:"name,omitempty"` | ||
|
|
||
| // +optional | ||
| // Slug of the tag | ||
| Slug string `json:"slug,omitempty"` | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| Copyright 2024 Swisscom (Schweiz) AG. | ||
| 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 api | ||
|
|
||
| import ( | ||
| "github.com/netbox-community/go-netbox/v3/netbox/client/extras" | ||
|
|
||
| "github.com/netbox-community/netbox-operator/pkg/netbox/models" | ||
| "github.com/netbox-community/netbox-operator/pkg/netbox/utils" | ||
| ) | ||
|
|
||
| func (r *NetboxClient) GetTagDetails(name string, slug string) (*models.Tag, error) { | ||
| var request *extras.ExtrasTagsListParams | ||
| if name != "" { | ||
| request = extras.NewExtrasTagsListParams().WithName(&name) | ||
| } | ||
| if slug != "" { | ||
| request = extras.NewExtrasTagsListParams().WithSlug(&slug) | ||
| } | ||
|
|
||
| if name == "" && slug == "" { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case should be prevented during creation on kube-api using XValidation rule in the api/CRD. |
||
| return nil, utils.NetboxError("either name or slug must be provided to fetch Tag details", nil) | ||
| } | ||
| // response, err := r.Tags.ExtrasTagsList(request, nil) | ||
| response, err := r.Extras.ExtrasTagsList(request, nil) | ||
| if err != nil { | ||
| return nil, utils.NetboxError("failed to fetch Tag details", err) | ||
| } | ||
|
|
||
| if len(response.Payload.Results) == 0 { | ||
| return nil, utils.NetboxNotFoundError("tag '" + name + "/" + slug + "'") | ||
| } | ||
|
|
||
| tag := response.Payload.Results[0] | ||
| return &models.Tag{ | ||
| Id: tag.ID, | ||
| Name: *tag.Name, | ||
| Slug: *tag.Slug, | ||
| }, nil | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| Copyright 2024 Swisscom (Schweiz) AG. | ||
|
|
||
| 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 api | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/netbox-community/go-netbox/v3/netbox/client/extras" | ||
| netboxModels "github.com/netbox-community/go-netbox/v3/netbox/models" | ||
| "github.com/netbox-community/netbox-operator/gen/mock_interfaces" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| func TestTags_GetTagDetailsByName(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) | ||
|
|
||
| tagName := "myTag" | ||
| tagSlug := "mytag" | ||
| tagId := int64(1) | ||
|
|
||
| tagListRequestInput := extras.NewExtrasTagsListParams().WithName(&tagName) | ||
| tagListOutput := &extras.ExtrasTagsListOK{ | ||
| Payload: &extras.ExtrasTagsListOKBody{ | ||
| Results: []*netboxModels.Tag{ | ||
| { | ||
| ID: tagId, | ||
| Name: &tagName, | ||
| Slug: &tagSlug, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| mockExtras.EXPECT().ExtrasTagsList(tagListRequestInput, nil).Return(tagListOutput, nil) | ||
| netboxClient := &NetboxClient{Extras: mockExtras} | ||
|
|
||
| actual, err := netboxClient.GetTagDetails(tagName, "") | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tagName, actual.Name) | ||
| assert.Equal(t, tagId, actual.Id) | ||
| assert.Equal(t, tagSlug, actual.Slug) | ||
| } | ||
|
|
||
| func TestTags_GetTagDetailsBySlug(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) | ||
|
|
||
| tagName := "myTag" | ||
| tagSlug := "mytag" | ||
| tagId := int64(1) | ||
|
|
||
| tagListRequestInput := extras.NewExtrasTagsListParams().WithSlug(&tagSlug) | ||
| tagListOutput := &extras.ExtrasTagsListOK{ | ||
| Payload: &extras.ExtrasTagsListOKBody{ | ||
| Results: []*netboxModels.Tag{ | ||
| { | ||
| ID: tagId, | ||
| Name: &tagName, | ||
| Slug: &tagSlug, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| mockExtras.EXPECT().ExtrasTagsList(tagListRequestInput, nil).Return(tagListOutput, nil) | ||
| netboxClient := &NetboxClient{Extras: mockExtras} | ||
|
|
||
| actual, err := netboxClient.GetTagDetails("", tagSlug) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tagName, actual.Name) | ||
| assert.Equal(t, tagId, actual.Id) | ||
| assert.Equal(t, tagSlug, actual.Slug) | ||
| } | ||
|
|
||
| func TestTags_GetTagDetailsNotFound(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) | ||
|
|
||
| tagName := "notfound" | ||
| tagListRequestInput := extras.NewExtrasTagsListParams().WithName(&tagName) | ||
| tagListOutput := &extras.ExtrasTagsListOK{ | ||
| Payload: &extras.ExtrasTagsListOKBody{ | ||
| Results: []*netboxModels.Tag{}, | ||
| }, | ||
| } | ||
|
|
||
| mockExtras.EXPECT().ExtrasTagsList(tagListRequestInput, nil).Return(tagListOutput, nil) | ||
| netboxClient := &NetboxClient{Extras: mockExtras} | ||
|
|
||
| actual, err := netboxClient.GetTagDetails(tagName, "") | ||
| assert.Nil(t, actual) | ||
| assert.EqualError(t, err, "failed to fetch tag 'notfound/': not found") | ||
| } | ||
|
|
||
| func TestTags_GetTagDetailsError(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| mockExtras := mock_interfaces.NewMockExtrasInterface(ctrl) | ||
|
|
||
| tagName := "error" | ||
| tagListRequestInput := extras.NewExtrasTagsListParams().WithName(&tagName) | ||
|
|
||
| mockExtras.EXPECT().ExtrasTagsList(tagListRequestInput, nil).Return(nil, errors.New("some error")) | ||
| netboxClient := &NetboxClient{Extras: mockExtras} | ||
|
|
||
| actual, err := netboxClient.GetTagDetails(tagName, "") | ||
| assert.Nil(t, actual) | ||
| assert.Contains(t, err.Error(), "failed to fetch Tag details") | ||
| } | ||
|
|
||
| func TestTags_GetTagDetailsNoInput(t *testing.T) { | ||
| netboxClient := &NetboxClient{} | ||
| actual, err := netboxClient.GetTagDetails("", "") | ||
| assert.Nil(t, actual) | ||
| assert.Contains(t, err.Error(), "either name or slug must be provided") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's either name or slug we should add a XValidation rule for this. The rule could probably be on the
Tagstruct.