github.com/vmware/govmomi@v0.37.2/vapi/library/security_policy.go (about)

     1  /*
     2  Copyright (c) 2022-2022 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package library
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"net/http"
    23  
    24  	"github.com/vmware/govmomi/vapi/internal"
    25  )
    26  
    27  const (
    28  	OvfDefaultSecurityPolicy = "OVF default policy"
    29  )
    30  
    31  // ContentSecurityPoliciesInfo contains information on security policies that can
    32  // be used to describe security for content library items.
    33  type ContentSecurityPoliciesInfo struct {
    34  	// ItemTypeRules are rules governing the policy.
    35  	ItemTypeRules map[string]string `json:"item_type_rules"`
    36  	// Name is a human-readable identifier identifying the policy.
    37  	Name string `json:"name"`
    38  	// Policy is the unique identifier for a policy.
    39  	Policy string `json:"policy"`
    40  }
    41  
    42  // ListSecurityPolicies lists security policies
    43  func (c *Manager) ListSecurityPolicies(ctx context.Context) ([]ContentSecurityPoliciesInfo, error) {
    44  	url := c.Resource(internal.SecurityPoliciesPath)
    45  	var res []ContentSecurityPoliciesInfo
    46  	return res, c.Do(ctx, url.Request(http.MethodGet), &res)
    47  }
    48  
    49  func (c *Manager) DefaultOvfSecurityPolicy(ctx context.Context) (string, error) {
    50  	res, err := c.ListSecurityPolicies(ctx)
    51  
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	for _, policy := range res {
    57  		if policy.Name == OvfDefaultSecurityPolicy {
    58  			return policy.Policy, nil
    59  		}
    60  	}
    61  
    62  	return "", errors.New("failed to find default ovf security policy")
    63  }