...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/util/cloudsqlutil.go

Documentation: github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/util

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package util contains utility functions for use throughout the Cloud SQL Auth proxy.
    16  package util
    17  
    18  import "strings"
    19  
    20  // SplitName splits a fully qualified instance into its project, region, and
    21  // instance name components. While we make the transition to regionalized
    22  // metadata, the region is optional.
    23  //
    24  // Examples:
    25  //    "proj:region:my-db" -> ("proj", "region", "my-db")
    26  //		"google.com:project:region:instance" -> ("google.com:project", "region", "instance")
    27  //		"google.com:missing:part" -> ("google.com:missing", "", "part")
    28  func SplitName(instance string) (project, region, name string) {
    29  	spl := strings.Split(instance, ":")
    30  	if len(spl) < 2 {
    31  		return "", "", instance
    32  	}
    33  	if dot := strings.Index(spl[0], "."); dot != -1 {
    34  		spl[1] = spl[0] + ":" + spl[1]
    35  		spl = spl[1:]
    36  	}
    37  	switch {
    38  	case len(spl) < 2:
    39  		return "", "", instance
    40  	case len(spl) == 2:
    41  		return spl[0], "", spl[1]
    42  	default:
    43  		return spl[0], spl[1], spl[2]
    44  	}
    45  }
    46  

View as plain text