-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calendar helper scripts for testing #17798
Merged
getvictor
merged 1 commit into
17230-calendar-feature
from
victor/17230-calendar-helper-scripts
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"golang.org/x/oauth2/google" | ||
"golang.org/x/oauth2/jwt" | ||
"google.golang.org/api/calendar/v3" | ||
"google.golang.org/api/option" | ||
"log" | ||
"os" | ||
) | ||
|
||
// Delete all events with eventTitle from the primary calendar of the user. | ||
|
||
var ( | ||
serviceEmail = os.Getenv("FLEET_TEST_GOOGLE_CALENDAR_SERVICE_EMAIL") | ||
privateKey = os.Getenv("FLEET_TEST_GOOGLE_CALENDAR_PRIVATE_KEY") | ||
) | ||
|
||
const ( | ||
eventTitle = "💻🚫Downtime" | ||
) | ||
|
||
func main() { | ||
if serviceEmail == "" || privateKey == "" { | ||
log.Fatal("FLEET_TEST_GOOGLE_CALENDAR_SERVICE_EMAIL and FLEET_TEST_GOOGLE_CALENDAR_PRIVATE_KEY must be set") | ||
} | ||
userEmail := flag.String("user", "", "User email to impersonate") | ||
flag.Parse() | ||
if *userEmail == "" { | ||
log.Fatal("--user is required") | ||
} | ||
|
||
ctx := context.Background() | ||
conf := &jwt.Config{ | ||
Email: serviceEmail, | ||
Scopes: []string{ | ||
"https://www.googleapis.com/auth/calendar.events", "https://www.googleapis.com/auth/calendar.settings.readonly", | ||
}, | ||
PrivateKey: []byte(privateKey), | ||
TokenURL: google.JWTTokenURL, | ||
Subject: *userEmail, | ||
} | ||
client := conf.Client(ctx) | ||
// Create a new calendar service | ||
service, err := calendar.NewService(ctx, option.WithHTTPClient(client)) | ||
if err != nil { | ||
log.Fatalf("Unable to create Calendar service: %v", err) | ||
} | ||
numberDeleted := 0 | ||
for { | ||
list, err := service.Events.List("primary").EventTypes("default").MaxResults(1000).OrderBy("startTime").SingleEvents(true).ShowDeleted(false).Q(eventTitle).Do() | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve list of events: %v", err) | ||
} | ||
if len(list.Items) == 0 { | ||
break | ||
} | ||
for _, item := range list.Items { | ||
if item.Summary == eventTitle { | ||
err = service.Events.Delete("primary", item.Id).Do() | ||
if err != nil { | ||
log.Fatalf("Unable to delete event: %v", err) | ||
} | ||
numberDeleted++ | ||
if numberDeleted%10 == 0 { | ||
log.Printf("Deleted %d events", numberDeleted) | ||
} | ||
} | ||
} | ||
} | ||
log.Printf("DONE. Deleted %d events total", numberDeleted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"golang.org/x/oauth2/google" | ||
"golang.org/x/oauth2/jwt" | ||
"google.golang.org/api/calendar/v3" | ||
"google.golang.org/api/option" | ||
"log" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Move all events with eventTitle from the primary calendar of the user to the new time. | ||
// Only events in the future relative to the new event time are moved. In other words, if the current event time is in the past, it is not moved. | ||
|
||
var ( | ||
serviceEmail = os.Getenv("FLEET_TEST_GOOGLE_CALENDAR_SERVICE_EMAIL") | ||
privateKey = os.Getenv("FLEET_TEST_GOOGLE_CALENDAR_PRIVATE_KEY") | ||
) | ||
|
||
const ( | ||
eventTitle = "💻🚫Downtime" | ||
) | ||
|
||
func main() { | ||
if serviceEmail == "" || privateKey == "" { | ||
log.Fatal("FLEET_TEST_GOOGLE_CALENDAR_SERVICE_EMAIL and FLEET_TEST_GOOGLE_CALENDAR_PRIVATE_KEY must be set") | ||
} | ||
userEmails := flag.String("users", "", "Comma-separated list of user emails to impersonate") | ||
dateTimeStr := flag.String("datetime", "", "Event time in "+time.RFC3339+" format") | ||
flag.Parse() | ||
if *userEmails == "" { | ||
log.Fatal("--users are required") | ||
} | ||
if *dateTimeStr == "" { | ||
log.Fatal("--datetime is required") | ||
} | ||
dateTime, err := time.Parse(time.RFC3339, *dateTimeStr) | ||
if err != nil { | ||
log.Fatalf("Unable to parse datetime: %v", err) | ||
} | ||
dateTimeEndStr := dateTime.Add(30 * time.Minute).Format(time.RFC3339) | ||
userEmailList := strings.Split(*userEmails, ",") | ||
if len(userEmailList) == 0 { | ||
log.Fatal("No user emails provided") | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
var wg sync.WaitGroup | ||
|
||
for _, userEmail := range userEmailList { | ||
wg.Add(1) | ||
go func(userEmail string) { | ||
defer wg.Done() | ||
conf := &jwt.Config{ | ||
Email: serviceEmail, | ||
Scopes: []string{ | ||
"https://www.googleapis.com/auth/calendar.events", "https://www.googleapis.com/auth/calendar.settings.readonly", | ||
}, | ||
PrivateKey: []byte(privateKey), | ||
TokenURL: google.JWTTokenURL, | ||
Subject: userEmail, | ||
} | ||
client := conf.Client(ctx) | ||
// Create a new calendar service | ||
service, err := calendar.NewService(ctx, option.WithHTTPClient(client)) | ||
if err != nil { | ||
log.Fatalf("Unable to create Calendar service: %v", err) | ||
} | ||
|
||
numberMoved := 0 | ||
for { | ||
list, err := service.Events.List("primary").EventTypes("default"). | ||
MaxResults(1000). | ||
OrderBy("startTime"). | ||
SingleEvents(true). | ||
ShowDeleted(false). | ||
TimeMin(dateTimeEndStr). | ||
Q(eventTitle). | ||
Do() | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve list of events: %v", err) | ||
} | ||
if len(list.Items) == 0 { | ||
break | ||
} | ||
for _, item := range list.Items { | ||
if item.Summary == eventTitle { | ||
item.Start.DateTime = dateTime.Format(time.RFC3339) | ||
item.End.DateTime = dateTime.Add(30 * time.Minute).Format(time.RFC3339) | ||
_, err := service.Events.Update("primary", item.Id, item).Do() | ||
if err != nil { | ||
log.Fatalf("Unable to update event: %v", err) | ||
} | ||
numberMoved++ | ||
} | ||
} | ||
} | ||
log.Printf("Moved %d events for %s", numberMoved, userEmail) | ||
}(userEmail) | ||
} | ||
|
||
// Wait for all goroutines to finish | ||
wg.Wait() | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this only list future events? It could be lengthy is a user has a long calendar history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is filtered by eventTitle:
Q(eventTitle)
and returns a max of 1000 resultsMaxResults(1000)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on second thought, a non-issue for loadtesting