github.com/sacloud/iaas-api-go@v1.12.0/helper/query/server_user_name.go (about) 1 // Copyright 2016-2022 The sacloud/iaas-api-go Authors 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 query 16 17 import ( 18 "context" 19 20 "github.com/sacloud/iaas-api-go/types" 21 ) 22 23 // ServerDefaultUserName returns default admin user name from source archives/disks 24 func ServerDefaultUserName(ctx context.Context, zone string, reader *ServerSourceReader, serverID types.ID) (string, error) { 25 // read server 26 server, err := reader.ServerReader.Read(ctx, zone, serverID) 27 if err != nil { 28 return "", err 29 } 30 31 if len(server.Disks) == 0 { 32 return "", nil 33 } 34 35 return getSSHDefaultUserNameDiskRec(ctx, zone, reader, server.Disks[0].ID) 36 } 37 38 func getSSHDefaultUserNameDiskRec(ctx context.Context, zone string, reader *ServerSourceReader, diskID types.ID) (string, error) { 39 disk, err := reader.DiskReader.Read(ctx, zone, diskID) 40 if err != nil { 41 return "", err 42 } 43 if !disk.SourceDiskID.IsEmpty() { 44 return getSSHDefaultUserNameDiskRec(ctx, zone, reader, disk.SourceDiskID) 45 } 46 47 if !disk.SourceArchiveID.IsEmpty() { 48 return getSSHDefaultUserNameArchiveRec(ctx, zone, reader, disk.SourceArchiveID) 49 } 50 return "", nil 51 } 52 53 func getSSHDefaultUserNameArchiveRec(ctx context.Context, zone string, reader *ServerSourceReader, archiveID types.ID) (string, error) { 54 archive, err := reader.ArchiveReader.Read(ctx, zone, archiveID) 55 if err != nil { 56 return "", err 57 } 58 59 if archive.Scope == types.Scopes.Shared { 60 if archive.HasTag("distro-ubuntu") { 61 return "ubuntu", nil 62 } 63 64 if archive.HasTag("distro-rancheros") { 65 return "rancher", nil 66 } 67 68 if archive.HasTag("distro-k3os") { 69 return "rancher", nil 70 } 71 } 72 if !archive.SourceDiskID.IsEmpty() { 73 return getSSHDefaultUserNameDiskRec(ctx, zone, reader, archive.SourceDiskID) 74 } 75 76 if !archive.SourceArchiveID.IsEmpty() { 77 return getSSHDefaultUserNameArchiveRec(ctx, zone, reader, archive.SourceArchiveID) 78 } 79 return "", nil 80 }