// ParseStateID returns the host, service, and instance id from the given state
// id
349148
func ParseStateID(stateID string) (string, string, int, error) {
	parts := strings.SplitN(stateID, "-", 3)
	if len(parts) != 3 {
		return "", "", 0, ErrInvalidStateID
	}
	instanceID, err := strconv.Atoi(parts[2])
	if err != nil {
		return "", "", 0, ErrInvalidStateID
	}
	return parts[0], parts[1], instanceID, nil
}
317042
func (api *ServerAPI) State(id int64) (string, error) {
	server, err := api.Read(id)
	if err != nil {
		return "", err
	}
	return string(server.Availability), nil
}
300247
func initHostPidId() {
	var sum [4]byte
	hostB := sum[:]
	host, err := os.Hostname()
	if err != nil {
		// if we cannot get hostname, just use a random set of bytes
		_, err2 := io.ReadFull(rand.Reader, hostB)
		if err2 != nil {
			panic(fmt.Errorf("cannot get hostname: %v; %v", err, err2))
		}
	} else {
		hw := md5.New()
		hw.Write([]byte(host))
		copy(hostB, hw.Sum(nil))
	}
	pid := os.Getpid()
	hostI := binary.BigEndian.Uint32(hostB)
	uid := uint32(pid) + uint32(hostI)
	binary.BigEndian.PutUint32(hostB, uid)
	b := make([]byte, 4)
	binary.BigEndian.PutUint32(b, uid)
	hostPidId = b[:]
}
1213543
func (l *State) serviceToken(id string) string {
	var token string
	if s := l.services[id]; s != nil {
		token = s.Token
	}
	if token == "" {
		token = l.tokens.UserToken()
	}
	return token
}
982098
func InstanceID(ctx types.Context) (*types.InstanceID, error) {
	req, err := http.NewRequest(http.MethodGet, iidURL, nil)
	if err != nil {
		return nil, err
	}

	res, err := doRequest(ctx, req)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	iid := instanceIdentityDoc{}
	dec := json.NewDecoder(res.Body)
	if err := dec.Decode(&iid); err != nil {
		return nil, err
	}

	mac, err := getMAC(ctx)
	if err != nil {
		return nil, err
	}

	subnetID, err := ResolveSubnetWithMAC(ctx, mac)
	if err != nil {
		return nil, err
	}

	secGroups, err := getSecurityGroups(ctx, mac)
	if err != nil {
		return nil, err
	}

	iidFields := map[string]string{
		efs.InstanceIDFieldRegion:           iid.Region,
		efs.InstanceIDFieldAvailabilityZone: iid.AvailabilityZone,
	}

	if len(secGroups) > 0 {
		iidFields[efs.InstanceIDFieldSecurityGroups] = strings.Join(
			secGroups, ";")
	}

	return &types.InstanceID{
		ID:     subnetID,
		Driver: efs.Name,
		Fields: iidFields,
	}, nil
}
1364550
func InstanceID(
	ctx types.Context, config gofig.Config) (*types.InstanceID, error) {

	var hostName string
	if config == nil {
		hostName = config.GetString(s3fs.ConfigS3FSHostName)
	} else {
		hostName, _ = os.Hostname()
	}
	return &types.InstanceID{ID: hostName, Driver: s3fs.Name}, nil
}
251341
func (l *State) ServiceStates() map[string]*ServiceState {
	l.RLock()
	defer l.RUnlock()

	m := make(map[string]*ServiceState)
	for id, s := range l.services {
		if s.Deleted {
			continue
		}
		m[id] = s.Clone()
	}
	return m
}
236134
func (sp *ServiceProvider) Parse(w http.ResponseWriter, r *http.Request) (*Assertion, error) {
	allowIdPInitiated := ""
	possibleRequestIDs := []string{allowIdPInitiated}

	// Find the request id that relates to this RelayState.
	relayState := r.PostFormValue("RelayState")
	if sp.cookieSecret() != nil && relayState != "" {
		cookieName := fmt.Sprintf("saml_%s", relayState)
		cookie, err := r.Cookie(cookieName)
		if err != nil {
			return nil, fmt.Errorf("cannot find %s cookie", cookieName)
		}

		// Verify the integrity of the cookie.
		state, err := jwt.Parse(cookie.Value, func(t *jwt.Token) (interface{}, error) {
			return sp.cookieSecret(), nil
		})
		if err != nil || !state.Valid {
			return nil, fmt.Errorf("could not decode state JWT: %v", err)
		}

		claims := state.Claims.(jwt.MapClaims)
		id := claims["id"].(string)

		possibleRequestIDs = append(possibleRequestIDs, id)

		// delete the cookie
		cookie.Value = ""
		cookie.Expires = time.Time{}
		http.SetCookie(w, cookie)
	}

	samlResponse := r.PostFormValue("SAMLResponse")
	return sp.ParseSAMLResponse(samlResponse, possibleRequestIDs)
}
1293653
func InstanceID(ctx types.Context) (*types.InstanceID, error) {

	id, err := getURL(ctx, metadataID)
	if err != nil {
		return nil, err
	}

	region, err := getURL(ctx, metadataRegion)
	if err != nil {
		return nil, err
	}

	name, err := getURL(ctx, metadataName)
	if err != nil {
		return nil, err
	}

	return &types.InstanceID{
		ID:     id,
		Driver: dobs.Name,
		Fields: map[string]string{
			dobs.InstanceIDFieldRegion: region,
			dobs.InstanceIDFieldName:   name,
		},
	}, nil
}
619051
func (l *HostStateListener) getExistingThreadStateIDs() []string {
	stateIds := make([]string, len(l.threads))
	i := 0
	for s := range l.threads {
		stateIds[i] = s
		i++
	}
	return stateIds
}
1014706
func (p *parser) parseStateCodeExpr(state *stateCodeExpr) (interface{}, bool) {
	// ==template== {{ if not .Optimize }}
	if p.debug {
		defer p.out(p.in("parseStateCodeExpr"))
	}

	// {{ end }} ==template==
	err := state.run(p)
	if err != nil {
		p.addErr(err)
	}
	return nil, true
}
818836
func (m *MockMachine) InstanceId() (instance.Id, error) {
	ret := m.ctrl.Call(m, "InstanceId")
	ret0, _ := ret[0].(instance.Id)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}
115041
func GetServiceStateHostID(conn client.Connection, poolID, serviceID string, instanceID int) (string, error) {
	logger := plog.WithFields(log.Fields{
		"serviceid":  serviceID,
		"instanceid": instanceID,
	})

	basepth := "/"
	if poolID != "" {
		basepth = path.Join("/pools", poolID)
	}

	spth := path.Join(basepth, "/services", serviceID)
	ch, err := conn.Children(spth)
	if err != nil && err != client.ErrNoNode {

		logger.WithError(err).Debug("Could not look up states on service")

		// TODO: wrap the error?
		return "", err
	}

	// find the matching state id
	suffix := fmt.Sprintf("-%s-%d", serviceID, instanceID)
	for _, stateID := range ch {
		if strings.HasSuffix(stateID, suffix) {
			hostID, _, _, err := ParseStateID(stateID)
			if err != nil {
				// This should never happen, but handle it anyway
				logger.WithField("stateid", stateID).WithError(err).Debug("Could not parse state")
				return "", err
			}
			logger.WithField("hostid", hostID).Debug("Found state id")
			return hostID, nil
		}
	}

	return "", ErrInstanceNotFound
}
275591
func ResourceID(arn string) (string, error) {
	a, err := Parse(arn)
	if err != nil {
		return "", err
	}

	_, id, err := SplitResource(a.Resource)
	return id, err
}
305005
func ParseResID(s string) (ResID, error) {
	if !IsResID(s) {
		return 0, fmt.Errorf("androidbinary: %s is not ResID", s)
	}
	id, err := strconv.ParseUint(s[3:], 16, 32)
	if err != nil {
		return 0, err
	}
	return ResID(id), nil
}
558641
func ParseIngressID(svc *types.Ingress) ServiceID {
	id := ServiceID{
		Namespace: svc.ObjectMeta.Namespace,
	}

	if svc.Spec.Backend != nil {
		id.Name = svc.Spec.Backend.ServiceName
	}

	return id
}
1243286
func ParseIPID(ipid string) (hostid string, ipaddress string, err error) {
	parts := strings.SplitN(ipid, "-", 2)
	if len(parts) != 2 {
		// TODO: error
		return "", "", ErrInvalidIPID
	}
	return parts[0], parts[1], nil
}