github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/container/container.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package container
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/juju/core/instance"
    10  )
    11  
    12  // ParentId returns the id of the host machine if machineId a container id, or ""
    13  // if machineId is not for a container.
    14  func ParentId(machineId string) string {
    15  	idParts := strings.Split(machineId, "/")
    16  	if len(idParts) < 3 {
    17  		return ""
    18  	}
    19  	return strings.Join(idParts[:len(idParts)-2], "/")
    20  }
    21  
    22  // ContainerTypeFromId returns the container type if machineId is a container id, or ""
    23  // if machineId is not for a container.
    24  func ContainerTypeFromId(machineId string) instance.ContainerType {
    25  	idParts := strings.Split(machineId, "/")
    26  	if len(idParts) < 3 {
    27  		return instance.ContainerType("")
    28  	}
    29  	return instance.ContainerType(idParts[len(idParts)-2])
    30  }
    31  
    32  // NestingLevel returns how many levels of nesting exist for a machine id.
    33  func NestingLevel(machineId string) int {
    34  	idParts := strings.Split(machineId, "/")
    35  	return (len(idParts) - 1) / 2
    36  }
    37  
    38  // TopParentId returns the id of the top level host machine for a container id.
    39  func TopParentId(machineId string) string {
    40  	idParts := strings.Split(machineId, "/")
    41  	return idParts[0]
    42  }