Skip to content

Commit

Permalink
Merge pull request #3558 from akutz/feature/pbm-iofilters
Browse files Browse the repository at this point in the history
Support querying PBM profile for IOFILTERS
  • Loading branch information
akutz authored Sep 20, 2024
2 parents 828cac6 + 9a02a4a commit 44d4fb3
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 99 deletions.
36 changes: 34 additions & 2 deletions pbm/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
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
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,
Expand Down Expand Up @@ -313,3 +313,35 @@ func (c *Client) QueryAssociatedProfiles(ctx context.Context, entities []types.P

return res.Returnval, nil
}

func (c *Client) QueryIOFiltersFromProfileId(
ctx context.Context, profileID string) ([]types.PbmProfileToIofilterMap, error) {

req := types.PbmQueryIOFiltersFromProfileId{
This: c.ServiceContent.ProfileManager,
ProfileIds: []types.PbmProfileId{{UniqueId: profileID}},
}
res, err := methods.PbmQueryIOFiltersFromProfileId(ctx, c, &req)
if err != nil {
return nil, err
}
return res.Returnval, nil
}

func (c *Client) SupportsEncryption(
ctx context.Context, profileID string) (bool, error) {

list, err := c.QueryIOFiltersFromProfileId(ctx, profileID)
if err != nil {
return false, err
}
for i := range list {
for j := range list[i].Iofilters {
f := list[i].Iofilters[j]
if f.FilterType == string(types.PbmIofilterInfoFilterTypeENCRYPTION) {
return true, nil
}
}
}
return false, nil
}
81 changes: 57 additions & 24 deletions pbm/client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
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
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,
Expand All @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package pbm
package pbm_test

import (
"context"
Expand All @@ -23,10 +23,17 @@ import (
"sort"
"testing"

"github.com/stretchr/testify/assert"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/fault"
"github.com/vmware/govmomi/pbm"
pbmsim "github.com/vmware/govmomi/pbm/simulator"
"github.com/vmware/govmomi/pbm/types"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
vim "github.com/vmware/govmomi/vim25/types"
Expand All @@ -52,7 +59,7 @@ func TestClient(t *testing.T) {
t.Fatal(err)
}

pc, err := NewClient(ctx, c.Client)
pc, err := pbm.NewClient(ctx, c.Client)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -164,27 +171,27 @@ func TestClient(t *testing.T) {

// user spec for the profile.
// VSAN profile with 2 capability instances - hostFailuresToTolerate = 2, stripeWidth = 1
pbmCreateSpecForVSAN := CapabilityProfileCreateSpec{
pbmCreateSpecForVSAN := pbm.CapabilityProfileCreateSpec{
Name: "Kubernetes-VSAN-TestPolicy",
Description: "VSAN Test policy create",
Category: string(types.PbmProfileCategoryEnumREQUIREMENT),
CapabilityList: []Capability{
Capability{
CapabilityList: []pbm.Capability{
{
ID: "hostFailuresToTolerate",
Namespace: "VSAN",
PropertyList: []Property{
Property{
PropertyList: []pbm.Property{
{
ID: "hostFailuresToTolerate",
Value: "2",
DataType: "int",
},
},
},
Capability{
{
ID: "stripeWidth",
Namespace: "VSAN",
PropertyList: []Property{
Property{
PropertyList: []pbm.Property{
{
ID: "stripeWidth",
Value: "1",
DataType: "int",
Expand All @@ -195,7 +202,7 @@ func TestClient(t *testing.T) {
}

// Create PBM capability spec for the above defined user spec.
createSpecVSAN, err := CreateCapabilityProfileSpec(pbmCreateSpecForVSAN)
createSpecVSAN, err := pbm.CreateCapabilityProfileSpec(pbmCreateSpecForVSAN)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -229,37 +236,37 @@ func TestClient(t *testing.T) {

// user spec for the profile.
// VSAN profile with 2 capability instances - stripeWidth = 1 and an SIOC profile.
pbmCreateSpecVSANandSIOC := CapabilityProfileCreateSpec{
pbmCreateSpecVSANandSIOC := pbm.CapabilityProfileCreateSpec{
Name: "Kubernetes-VSAN-SIOC-TestPolicy",
Description: "VSAN-SIOC-Test policy create",
Category: string(types.PbmProfileCategoryEnumREQUIREMENT),
CapabilityList: []Capability{
Capability{
CapabilityList: []pbm.Capability{
{
ID: "stripeWidth",
Namespace: "VSAN",
PropertyList: []Property{
Property{
PropertyList: []pbm.Property{
{
ID: "stripeWidth",
Value: "1",
DataType: "int",
},
},
},
Capability{
{
ID: "spm@DATASTOREIOCONTROL",
Namespace: "spm",
PropertyList: []Property{
Property{
PropertyList: []pbm.Property{
{
ID: "limit",
Value: "200",
DataType: "int",
},
Property{
{
ID: "reservation",
Value: "1000",
DataType: "int",
},
Property{
{
ID: "shares",
Value: "2000",
DataType: "int",
Expand All @@ -270,7 +277,7 @@ func TestClient(t *testing.T) {
}

// Create PBM capability spec for the above defined user spec.
createSpecVSANandSIOC, err := CreateCapabilityProfileSpec(pbmCreateSpecVSANandSIOC)
createSpecVSANandSIOC, err := pbm.CreateCapabilityProfileSpec(pbmCreateSpecVSANandSIOC)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -313,3 +320,29 @@ func TestClient(t *testing.T) {
}
t.Logf("Profile: %+v successfully deleted", []types.PbmProfileId{*vsanProfileID, *vsansiocProfileID})
}

func TestSupportsEncryption(t *testing.T) {
t.Run("valid profile id", func(t *testing.T) {
simulator.Test(func(ctx context.Context, c *vim25.Client) {
pbmc, err := pbm.NewClient(ctx, c)
assert.NoError(t, err)
assert.NotNil(t, pbmc)

ok, err := pbmc.SupportsEncryption(ctx, pbmsim.DefaultEncryptionProfileID)
assert.NoError(t, err)
assert.True(t, ok)
})
})
t.Run("invalid profile id", func(t *testing.T) {
simulator.Test(func(ctx context.Context, c *vim25.Client) {
pbmc, err := pbm.NewClient(ctx, c)
assert.NoError(t, err)
assert.NotNil(t, pbmc)

ok, err := pbmc.SupportsEncryption(ctx, "invalid")
assert.EqualError(t, err, "ServerFaultCode: Invalid profile ID")
assert.True(t, fault.Is(err, &vim.RuntimeFault{}))
assert.False(t, ok)
})
})
}
44 changes: 44 additions & 0 deletions pbm/methods/internal_methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
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 methods

import (
"context"

"github.com/vmware/govmomi/pbm/types"
"github.com/vmware/govmomi/vim25/soap"
)

type PbmQueryIOFiltersFromProfileIdBody struct {
Req *types.PbmQueryIOFiltersFromProfileId `xml:"urn:pbm PbmQueryIOFiltersFromProfileId,omitempty"`
Res *types.PbmQueryIOFiltersFromProfileIdResponse `xml:"urn:pbm PbmQueryIOFiltersFromProfileIdResponse,omitempty"`
Fault_ *soap.Fault `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`
}

func (b *PbmQueryIOFiltersFromProfileIdBody) Fault() *soap.Fault { return b.Fault_ }

func PbmQueryIOFiltersFromProfileId(ctx context.Context, r soap.RoundTripper, req *types.PbmQueryIOFiltersFromProfileId) (*types.PbmQueryIOFiltersFromProfileIdResponse, error) {
var reqBody, resBody PbmQueryIOFiltersFromProfileIdBody

reqBody.Req = req

if err := r.RoundTrip(ctx, &reqBody, &resBody); err != nil {
return nil, err
}

return resBody.Res, nil
}
Loading

0 comments on commit 44d4fb3

Please sign in to comment.