github.com/midokura/kubeedge@v1.2.0-mido.0/tests/stubs/common/utils/utils.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     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 utils
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/kubeedge/beehive/pkg/core/model"
    24  	constants "github.com/kubeedge/kubeedge/common/constants"
    25  	testconstants "github.com/kubeedge/kubeedge/tests/stubs/common/constants"
    26  )
    27  
    28  // BuildResource return a string as "beehive/pkg/core/model".Message.Router.Resource
    29  func BuildResource(nodeID, namespace, resourceType, resourceID string) (resource string, err error) {
    30  	if nodeID == "" || namespace == "" || resourceType == "" {
    31  		err = fmt.Errorf("Required parameter are not set (node id, namespace or resource type)")
    32  		return
    33  	}
    34  	resource = fmt.Sprintf("%s%s%s%s%s%s%s", testconstants.ResourceNode, constants.ResourceSep, nodeID, constants.ResourceSep, namespace, constants.ResourceSep, resourceType)
    35  	if resourceID != "" {
    36  		resource += fmt.Sprintf("%s%s", constants.ResourceSep, resourceID)
    37  	}
    38  	return
    39  }
    40  
    41  // GetNodeID from "beehive/pkg/core/model".Message.Router.Resource
    42  func GetNodeID(msg model.Message) (string, error) {
    43  	sli := strings.Split(msg.GetResource(), constants.ResourceSep)
    44  	if len(sli) <= testconstants.ResourceNodeIDIndex {
    45  		return "", fmt.Errorf("Node id not found")
    46  	}
    47  	return sli[testconstants.ResourceNodeIDIndex], nil
    48  }
    49  
    50  // GetNamespace from "beehive/pkg/core/model".Model.Router.Resource
    51  func GetNamespace(msg model.Message) (string, error) {
    52  	sli := strings.Split(msg.GetResource(), constants.ResourceSep)
    53  	if len(sli) <= testconstants.ResourceNamespaceIndex {
    54  		return "", fmt.Errorf("Namespace not found")
    55  	}
    56  	return sli[testconstants.ResourceNamespaceIndex], nil
    57  }
    58  
    59  // GetResourceType from "beehive/pkg/core/model".Model.Router.Resource
    60  func GetResourceType(msg model.Message) (string, error) {
    61  	sli := strings.Split(msg.GetResource(), constants.ResourceSep)
    62  	if len(sli) <= testconstants.ResourceResourceTypeIndex {
    63  		return "", fmt.Errorf("Resource type not found")
    64  	}
    65  	return sli[testconstants.ResourceResourceTypeIndex], nil
    66  }
    67  
    68  // GetResourceName from "beehive/pkg/core/model".Model.Router.Resource
    69  func GetResourceName(msg model.Message) (string, error) {
    70  	sli := strings.Split(msg.GetResource(), constants.ResourceSep)
    71  	if len(sli) <= testconstants.ResourceResourceNameIndex {
    72  		return "", fmt.Errorf("Resource name not found")
    73  	}
    74  	return sli[testconstants.ResourceResourceNameIndex], nil
    75  }
    76  
    77  // ParseResourceEdge parses resource at edge and returns namespace, resource_type, resource_id.
    78  // If operation of msg is query list, return namespace, pod.
    79  func ParseResourceEdge(resource string, operation string) (string, string, string, error) {
    80  	resourceSplits := strings.Split(resource, "/")
    81  	if len(resourceSplits) == 3 {
    82  		return resourceSplits[0], resourceSplits[1], resourceSplits[2], nil
    83  	} else if operation == model.QueryOperation || operation == model.ResponseOperation && len(resourceSplits) == 2 {
    84  		return resourceSplits[0], resourceSplits[1], "", nil
    85  	} else {
    86  		return "", "", "", fmt.Errorf("Resource: %s format incorrect, or Operation: %s is not query/response", resource, operation)
    87  	}
    88  }