Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new API token for a Jenkins user.
Jenkins
def create_api_token(user): token = user.[1]('new-token') return token
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'createToken' which is not a valid Jenkins user method.
Using 'newToken' or 'makeToken' which do not exist.
✗ Incorrect
The correct method to generate a new API token in Jenkins user object is generateToken.
2fill in blank
mediumComplete the code to revoke an existing API token by its name.
Jenkins
def revoke_api_token(user, token_name): tokens = user.getApiTokens() tokens.[1](token_name) user.save()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'removeToken' which is not a Jenkins API method.
Using 'revokeToken' or 'clearToken' which do not exist.
✗ Incorrect
The method to delete an API token by name in Jenkins is deleteToken.
3fill in blank
hardFix the error in the code to list all API tokens for a Jenkins user.
Jenkins
def list_tokens(user): tokens = user.[1]() for token in tokens: print(token.name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listTokens' which is not a valid method.
Using 'fetchTokens' or 'retrieveTokens' which do not exist.
✗ Incorrect
The correct method to get all API tokens from a Jenkins user is getApiTokens.
4fill in blank
hardFill both blanks to check if a token exists and then revoke it.
Jenkins
def revoke_if_exists(user, token_name): tokens = user.getApiTokens() if token_name [1] tokens: tokens.[2](token_name) user.save()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' instead of 'in' for the check.
Using 'removeToken' which is not a valid method.
✗ Incorrect
Use in to check if the token exists, then deleteToken to revoke it.
5fill in blank
hardFill all three blanks to create a token, print its value, and save the user.
Jenkins
def create_and_show_token(user, token_name): token = user.[1](token_name) print(token.[2]) user.[3]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tokenValue' instead of 'value' property.
Forgetting to call 'save' on the user.
✗ Incorrect
Use generateToken to create, value to get the token string, and save to persist changes.