Skip to content

Commit 424a14c

Browse files
dwightjlengelke
authored andcommitted
Improvements to facilitate the documentation instructions (GoogleCloudPlatform#2036)
* Improvements to facilitate the documentation instructions - Add hostname and account arguments to the argument parser. - Modify main to accommodate the new arguments. These changes allow users to run this example outside of a Compute Engine environment if they want. * Clarify argument assignments
1 parent 1f1dee7 commit 424a14c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

compute/oslogin/service_account_ssh.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ def run_ssh(cmd, private_key_file, username, hostname):
9797

9898

9999
# [START main]
100-
def main(
101-
cmd, project, instance, zone, oslogin=None, account=None, hostname=None
102-
):
100+
def main(cmd, project, instance=None, zone=None,
101+
oslogin=None, account=None, hostname=None):
103102
"""Run a command on a remote system."""
104103

105104
# Create the OS Login API object.
106105
oslogin = oslogin or googleapiclient.discovery.build('oslogin', 'v1')
107106

108107
# Identify the service account ID if it is not already provided.
109-
account = account or 'users/' + requests.get(
108+
account = account or requests.get(
110109
SERVICE_ACCOUNT_METADATA_URL, headers=HEADERS).text
110+
account = 'users/' + account
111111

112112
# Create a new SSH key pair and associate it with the service account.
113113
private_key_file = create_ssh_key(oslogin, account)
@@ -146,16 +146,23 @@ def main(
146146
'--cmd', default='uname -a',
147147
help='The command to run on the remote instance.')
148148
parser.add_argument(
149-
'--project', default='my-project-id',
149+
'--project',
150150
help='Your Google Cloud project ID.')
151151
parser.add_argument(
152-
'--zone', default='us-central1-f',
152+
'--zone',
153153
help='The zone where the target instance is locted.')
154154
parser.add_argument(
155-
'--instance', default='my-instance-name',
155+
'--instance',
156156
help='The target instance for the ssh command.')
157+
parser.add_argument(
158+
'--account',
159+
help='The service account email.')
160+
parser.add_argument(
161+
'--hostname',
162+
help='The external IP address or hostname for the target instance.')
157163
args = parser.parse_args()
158164

159-
main(args.cmd, args.project, args.instance, args.zone)
165+
main(args.cmd, args.project, instance=args.instance, zone=args.zone,
166+
account=args.account, hostname=args.hostname)
160167

161168
# [END main]

0 commit comments

Comments
 (0)