Skip to content

Commit 4db8d94

Browse files
committed
Added New-WattTimeAccount function
1 parent b393e51 commit 4db8d94

3 files changed

Lines changed: 83 additions & 15 deletions

File tree

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,42 @@ For a real-world solution with a commercial data provider, check out
2727
the [carbon-aware-sdk](https://github.com/Green-Software-Foundation/carbon-aware-sdk)
2828
by the [Green Software Foundation](https://greensoftware.foundation/)
2929

30-
This module is supplied as-is at the moment and not (yet)
31-
published through _PSGallery_.
32-
3330
### Usage
3431

3532
#### PowerShell Module
3633

3734
To get started with this module you will need an account on
3835
[WattTime](https://www.watttime.org/). See
3936
[the manual](https://www.watttime.org/api-documentation/#register-new-user)
40-
on registering an account. NOTE: You can only register via the API.
41-
Currently no GUI exists for registration.
37+
on registering an account. The module provides a function `New-WattTimeAccount`
38+
to create an account if you do not yet have one.
4239

4340
You will also need the _Az.Resources_ PowerShell Module for Azure installed
4441
and connected to your Azure account. See
4542
[the installation manual](https://learn.microsoft.com/en-us/powershell/azure/install-az-ps)
4643
for the _Az_ module for instructions.
4744

48-
Import the module from a clone of this repository:
45+
To install the module from the [PowerShell Gallery](https://www.powershellgallery.com/packages/PSWattTime/).
46+
47+
```powershell
48+
Install-Module -Name PSWattTime
49+
```
50+
51+
To register an account with _WattTime_:
4952

5053
```powershell
51-
Import-Module .\src\PSWattTime
54+
New-WattTimeAccount -Username <YOUR_USERNAME> `
55+
-Password '<YOUR_PASSWORD>' `
56+
-Email '<you@domain.com>' `
57+
-Organization <YOUR_ORGANIZATION>
5258
```
5359

60+
5461
Authenticate to the _WattTime_ API:
5562

5663
```powershell
57-
$token = Get-WattTimeAuthToken -Username '<YOUR_WATTTIME_USERNAME>' -Password '<YOUR_WATTTIME_PASSWORD>'
64+
$token = Get-WattTimeAuthToken -Username '<YOUR_WATTTIME_USERNAME>' `
65+
-Password '<YOUR_WATTTIME_PASSWORD>'
5866
```
5967

6068
Get current percentage of energy with emissions the 'westeurope' Azure region:

src/PSWattTime/PSWattTime.psd1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSWattTime.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.1.0'
15+
ModuleVersion = '1.0.2.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -30,7 +30,7 @@ CompanyName = 'Unknown'
3030
Copyright = '(c) Roderick Bant. All rights reserved.'
3131

3232
# Description of the functionality provided by this module
33-
Description = 'Simple PowerShell interface getting WattTime emissions data for Azure regions'
33+
Description = 'Simple PowerShell interface for getting WattTime emissions data for Azure regions'
3434

3535
# Minimum version of the PowerShell engine required by this module
3636
# PowerShellVersion = ''
@@ -69,7 +69,14 @@ RequiredModules = @('Az.Resources')
6969
# NestedModules = @()
7070

7171
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72-
FunctionsToExport = @('Get-WattTime', 'Get-WattTimeAuthToken', 'ConvertFrom-AzureRegion', 'Get-WattTimeForAzureRegion', 'Get-AzureRegionWithLowestWattTime')
72+
FunctionsToExport = @(
73+
'Get-WattTime',
74+
'Get-WattTimeAuthToken',
75+
'ConvertFrom-AzureRegion',
76+
'Get-WattTimeForAzureRegion',
77+
'Get-AzureRegionWithLowestWattTime',
78+
'New-WattTimeAccount'
79+
)
7380

7481
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7582
CmdletsToExport = @()
@@ -107,7 +114,7 @@ PrivateData = @{
107114
# IconUri = ''
108115

109116
# ReleaseNotes of this module
110-
ReleaseNotes = 'Initial PSWattTime Release'
117+
ReleaseNotes = 'Added New-WattTimeAccount function for registering a new WattTime account'
111118

112119
# Prerelease string of this module
113120
# Prerelease = ''
@@ -123,10 +130,9 @@ PrivateData = @{
123130
} # End of PrivateData hashtable
124131

125132
# HelpInfo URI of this module
126-
# HelpInfoURI = ''
133+
HelpInfoURI = ''
127134

128135
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
129136
# DefaultCommandPrefix = ''
130137

131138
}
132-

src/PSWattTime/PSWattTime.psm1

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,59 @@ function Get-AzureRegionWithLowestWattTime {
166166
$wattTime | Sort-Object -Property percent | Select-Object -First 1
167167
}
168168

169-
Export-ModuleMember -Function Get-WattTimeAuthToken, Get-WattTime, ConvertFrom-AzureRegion, Get-WattTimeForAzureRegion, Get-AzureRegionWithLowestWattTime
169+
<#
170+
.SYNOPSIS
171+
Create a new WattTime account
172+
173+
.DESCRIPTION
174+
Creates a new PSWattTime account with the specified username,
175+
password, email and organization
176+
177+
.PARAMETER Username
178+
The username for the new account
179+
180+
.PARAMETER Password
181+
The password for the new account
182+
183+
.PARAMETER Email
184+
The email address for the new account
185+
186+
.PARAMETER Organization
187+
The organization for the new account
188+
189+
.EXAMPLE
190+
New-WattTimeAccount -Username 'username' -Password 'password' -Email 'email' -Organization 'organization'
191+
#>
192+
function New-WattTimeAccount {
193+
[CmdletBinding()]
194+
param (
195+
[Parameter(Mandatory=$true)]
196+
[string]$Username,
197+
[Parameter(Mandatory=$true)]
198+
[string]$Password,
199+
[Parameter(Mandatory=$true)]
200+
[string]$Email,
201+
[Parameter(Mandatory=$true)]
202+
[string]$Organization
203+
)
204+
$headers = @{
205+
'Content-Type' = 'application/json'
206+
}
207+
$body = @{
208+
username = $Username
209+
password = $Password
210+
email = $Email
211+
org = $Organization
212+
}
213+
$bodyJson = $body | ConvertTo-Json
214+
$response = Invoke-RestMethod -Uri 'https://api2.watttime.org/v2/register/' -Method Post -Headers $headers -Body $bodyJson -ContentType 'application/json'
215+
return $response
216+
}
170217

218+
Export-ModuleMember -Function `
219+
Get-WattTimeAuthToken, `
220+
Get-WattTime, `
221+
ConvertFrom-AzureRegion, `
222+
Get-WattTimeForAzureRegion, `
223+
Get-AzureRegionWithLowestWattTime, `
224+
New-WattTimeAccount

0 commit comments

Comments
 (0)