1# -*- coding: utf-8 -*- # 2# Copyright 2014 Google LLC. 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"""A module for generating resource names.""" 17 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import unicode_literals 21 22import io 23import random 24import string 25import six 26from six.moves import range # pylint: disable=redefined-builtin 27 28_LENGTH = 12 29_BEGIN_ALPHABET = string.ascii_lowercase 30_ALPHABET = _BEGIN_ALPHABET + string.digits 31 32 33def GenerateRandomName(): 34 """Generates a random string. 35 36 Returns: 37 The returned string will be 12 characters long and will begin with 38 a lowercase letter followed by 11 characters drawn from the set 39 [a-z0-9]. 40 """ 41 buf = io.StringIO() 42 buf.write(six.text_type(random.choice(_BEGIN_ALPHABET))) 43 for _ in range(_LENGTH - 1): 44 buf.write(six.text_type(random.choice(_ALPHABET))) 45 return buf.getvalue() 46