-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move providerID to the package
Parsing and generating magic providerID moved to a separate package. Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
- Loading branch information
1 parent
6f0c667
commit 7b73b5f
Showing
7 changed files
with
289 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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,77 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
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 provider implements the providerID interface for Proxmox. | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
) | ||
|
||
const ( | ||
// ProviderName is the name of the Proxmox provider. | ||
ProviderName = "proxmox" | ||
) | ||
|
||
var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `://([^/]*)/([^/]+)$`) | ||
|
||
// GetProviderID returns the magic providerID for kubernetes node. | ||
func GetProviderID(region string, vmr *pxapi.VmRef) string { | ||
return fmt.Sprintf("%s://%s/%d", ProviderName, region, vmr.VmId()) | ||
} | ||
|
||
// GetVMID returns the VM ID from the providerID. | ||
func GetVMID(providerID string) (int, error) { | ||
if !strings.HasPrefix(providerID, ProviderName) { | ||
return 0, fmt.Errorf("foreign providerID or empty \"%s\"", providerID) | ||
} | ||
|
||
matches := providerIDRegexp.FindStringSubmatch(providerID) | ||
if len(matches) != 3 { | ||
return 0, fmt.Errorf("providerID \"%s\" didn't match expected format \"%s://region/InstanceID\"", providerID, ProviderName) | ||
} | ||
|
||
vmID, err := strconv.Atoi(matches[2]) | ||
if err != nil { | ||
return 0, fmt.Errorf("InstanceID have to be a number, but got \"%s\"", matches[2]) | ||
} | ||
|
||
return vmID, nil | ||
} | ||
|
||
// ParseProviderID returns the VmRef and region from the providerID. | ||
func ParseProviderID(providerID string) (*pxapi.VmRef, string, error) { | ||
if !strings.HasPrefix(providerID, ProviderName) { | ||
return nil, "", fmt.Errorf("foreign providerID or empty \"%s\"", providerID) | ||
} | ||
|
||
matches := providerIDRegexp.FindStringSubmatch(providerID) | ||
if len(matches) != 3 { | ||
return nil, "", fmt.Errorf("providerID \"%s\" didn't match expected format \"%s://region/InstanceID\"", providerID, ProviderName) | ||
} | ||
|
||
vmID, err := strconv.Atoi(matches[2]) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("InstanceID have to be a number, but got \"%s\"", matches[2]) | ||
} | ||
|
||
return pxapi.NewVmRef(vmID), matches[1], nil | ||
} |
This file contains 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,193 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
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 provider_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/stretchr/testify/assert" | ||
|
||
provider "github.com/sergelogvinov/proxmox-cloud-controller-manager/pkg/provider" | ||
) | ||
|
||
func TestGetProviderID(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
msg string | ||
region string | ||
vmID int | ||
expectedProviderID string | ||
}{ | ||
{ | ||
msg: "Valid providerID", | ||
region: "region", | ||
vmID: 123, | ||
expectedProviderID: "proxmox://region/123", | ||
}, | ||
{ | ||
msg: "No region", | ||
region: "", | ||
vmID: 123, | ||
expectedProviderID: "proxmox:///123", | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
testCase := testCase | ||
|
||
t.Run(fmt.Sprint(testCase.msg), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
providerID := provider.GetProviderID(testCase.region, pxapi.NewVmRef(testCase.vmID)) | ||
|
||
assert.Equal(t, testCase.expectedProviderID, providerID) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetVmID(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
msg string | ||
providerID string | ||
expectedError error | ||
expectedvmID int | ||
}{ | ||
{ | ||
msg: "Valid VMID", | ||
providerID: "proxmox://region/123", | ||
expectedError: nil, | ||
expectedvmID: 123, | ||
}, | ||
{ | ||
msg: "Valid VMID with empty region", | ||
providerID: "proxmox:///123", | ||
expectedError: nil, | ||
expectedvmID: 123, | ||
}, | ||
{ | ||
msg: "Invalid providerID format", | ||
providerID: "proxmox://123", | ||
expectedError: fmt.Errorf("providerID \"proxmox://123\" didn't match expected format \"proxmox://region/InstanceID\""), | ||
}, | ||
{ | ||
msg: "Non proxmox providerID", | ||
providerID: "cloud:///123", | ||
expectedError: fmt.Errorf("foreign providerID or empty \"cloud:///123\""), | ||
expectedvmID: 123, | ||
}, | ||
{ | ||
msg: "Non proxmox providerID", | ||
providerID: "cloud://123", | ||
expectedError: fmt.Errorf("foreign providerID or empty \"cloud://123\""), | ||
expectedvmID: 123, | ||
}, | ||
{ | ||
msg: "InValid VMID", | ||
providerID: "proxmox://region/abc", | ||
expectedError: fmt.Errorf("InstanceID have to be a number, but got \"abc\""), | ||
expectedvmID: 0, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
testCase := testCase | ||
|
||
t.Run(fmt.Sprint(testCase.msg), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
VMID, err := provider.GetVMID(testCase.providerID) | ||
|
||
if testCase.expectedError != nil { | ||
assert.NotNil(t, err) | ||
assert.Equal(t, err.Error(), testCase.expectedError.Error()) | ||
} else { | ||
assert.Equal(t, testCase.expectedvmID, VMID) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseProviderID(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
msg string | ||
providerID string | ||
expectedError error | ||
expectedvmID int | ||
expectedRegion string | ||
}{ | ||
{ | ||
msg: "Valid VMID", | ||
providerID: "proxmox://region/123", | ||
expectedError: nil, | ||
expectedvmID: 123, | ||
expectedRegion: "region", | ||
}, | ||
{ | ||
msg: "Valid VMID with empty region", | ||
providerID: "proxmox:///123", | ||
expectedError: nil, | ||
expectedvmID: 123, | ||
expectedRegion: "", | ||
}, | ||
{ | ||
msg: "Invalid providerID format", | ||
providerID: "proxmox://123", | ||
expectedError: fmt.Errorf("providerID \"proxmox://123\" didn't match expected format \"proxmox://region/InstanceID\""), | ||
}, | ||
{ | ||
msg: "Non proxmox providerID", | ||
providerID: "cloud:///123", | ||
expectedError: fmt.Errorf("foreign providerID or empty \"cloud:///123\""), | ||
}, | ||
{ | ||
msg: "Non proxmox providerID", | ||
providerID: "cloud://123", | ||
expectedError: fmt.Errorf("foreign providerID or empty \"cloud://123\""), | ||
}, | ||
{ | ||
msg: "InValid VMID", | ||
providerID: "proxmox://region/abc", | ||
expectedError: fmt.Errorf("InstanceID have to be a number, but got \"abc\""), | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
testCase := testCase | ||
|
||
t.Run(fmt.Sprint(testCase.msg), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
vmr, region, err := provider.ParseProviderID(testCase.providerID) | ||
|
||
if testCase.expectedError != nil { | ||
assert.NotNil(t, err) | ||
assert.Equal(t, err.Error(), testCase.expectedError.Error()) | ||
} else { | ||
assert.NotNil(t, vmr) | ||
assert.Equal(t, testCase.expectedvmID, vmr.VmId()) | ||
assert.Equal(t, testCase.expectedRegion, region) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.