|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/jianyuan/terraform-provider-openai/internal/apiclient" |
| 12 | + "github.com/jianyuan/terraform-provider-openai/internal/ptr" |
| 13 | +) |
| 14 | + |
| 15 | +type ProjectRateLimitModel struct { |
| 16 | + Id types.String `tfsdk:"id"` |
| 17 | + Model types.String `tfsdk:"model"` |
| 18 | + MaxRequestsPer1Minute types.Int64 `tfsdk:"max_requests_per_1_minute"` |
| 19 | + MaxTokensPer1Minute types.Int64 `tfsdk:"max_tokens_per_1_minute"` |
| 20 | + MaxImagesPer1Minute types.Int64 `tfsdk:"max_images_per_1_minute"` |
| 21 | + MaxAudioMegabytesPer1Minute types.Int64 `tfsdk:"max_audio_megabytes_per_1_minute"` |
| 22 | + MaxRequestsPer1Day types.Int64 `tfsdk:"max_requests_per_1_day"` |
| 23 | + Batch1DayMaxInputTokens types.Int64 `tfsdk:"batch_1_day_max_input_tokens"` |
| 24 | +} |
| 25 | + |
| 26 | +func (m *ProjectRateLimitModel) Fill(rl apiclient.ProjectRateLimit) error { |
| 27 | + m.Id = types.StringValue(rl.Id) |
| 28 | + m.Model = types.StringValue(rl.Model) |
| 29 | + m.MaxRequestsPer1Minute = types.Int64Value(int64(rl.MaxRequestsPer1Minute)) |
| 30 | + m.MaxTokensPer1Minute = types.Int64Value(int64(rl.MaxTokensPer1Minute)) |
| 31 | + if rl.MaxImagesPer1Minute == nil { |
| 32 | + m.MaxImagesPer1Minute = types.Int64Null() |
| 33 | + } else { |
| 34 | + m.MaxImagesPer1Minute = types.Int64Value(int64(*rl.MaxImagesPer1Minute)) |
| 35 | + } |
| 36 | + if rl.MaxAudioMegabytesPer1Minute == nil { |
| 37 | + m.MaxAudioMegabytesPer1Minute = types.Int64Null() |
| 38 | + } else { |
| 39 | + m.MaxAudioMegabytesPer1Minute = types.Int64Value(int64(*rl.MaxAudioMegabytesPer1Minute)) |
| 40 | + } |
| 41 | + if rl.MaxRequestsPer1Day == nil { |
| 42 | + m.MaxRequestsPer1Day = types.Int64Null() |
| 43 | + } else { |
| 44 | + m.MaxRequestsPer1Day = types.Int64Value(int64(*rl.MaxRequestsPer1Day)) |
| 45 | + } |
| 46 | + if rl.Batch1DayMaxInputTokens == nil { |
| 47 | + m.Batch1DayMaxInputTokens = types.Int64Null() |
| 48 | + } else { |
| 49 | + m.Batch1DayMaxInputTokens = types.Int64Value(int64(*rl.Batch1DayMaxInputTokens)) |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +type ProjectRateLimitsDataSourceModel struct { |
| 55 | + ProjectId types.String `tfsdk:"project_id"` |
| 56 | + RateLimits []ProjectRateLimitModel `tfsdk:"rate_limits"` |
| 57 | +} |
| 58 | + |
| 59 | +func (m *ProjectRateLimitsDataSourceModel) Fill(rateLimits []apiclient.ProjectRateLimit) error { |
| 60 | + m.RateLimits = make([]ProjectRateLimitModel, len(rateLimits)) |
| 61 | + for i, rl := range rateLimits { |
| 62 | + if err := m.RateLimits[i].Fill(rl); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +var _ datasource.DataSource = &ProjectRateLimitsDataSource{} |
| 70 | + |
| 71 | +func NewProjectRateLimitsDataSource() datasource.DataSource { |
| 72 | + return &ProjectRateLimitsDataSource{} |
| 73 | +} |
| 74 | + |
| 75 | +type ProjectRateLimitsDataSource struct { |
| 76 | + baseDataSource |
| 77 | +} |
| 78 | + |
| 79 | +func (d *ProjectRateLimitsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 80 | + resp.TypeName = req.ProviderTypeName + "_project_rate_limits" |
| 81 | +} |
| 82 | + |
| 83 | +func (d *ProjectRateLimitsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 84 | + resp.Schema = schema.Schema{ |
| 85 | + MarkdownDescription: "Returns the rate limits per model for a project.", |
| 86 | + |
| 87 | + Attributes: map[string]schema.Attribute{ |
| 88 | + "project_id": schema.StringAttribute{ |
| 89 | + MarkdownDescription: "The ID of the project.", |
| 90 | + Required: true, |
| 91 | + }, |
| 92 | + "rate_limits": schema.SetNestedAttribute{ |
| 93 | + MarkdownDescription: "List of users.", |
| 94 | + Computed: true, |
| 95 | + NestedObject: schema.NestedAttributeObject{ |
| 96 | + Attributes: map[string]schema.Attribute{ |
| 97 | + "id": schema.StringAttribute{ |
| 98 | + MarkdownDescription: "The rate limit identifier.", |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + "model": schema.StringAttribute{ |
| 102 | + MarkdownDescription: "The model this rate limit applies to.", |
| 103 | + Computed: true, |
| 104 | + }, |
| 105 | + "max_requests_per_1_minute": schema.Int64Attribute{ |
| 106 | + MarkdownDescription: "The maximum requests per minute.", |
| 107 | + Computed: true, |
| 108 | + }, |
| 109 | + "max_tokens_per_1_minute": schema.Int64Attribute{ |
| 110 | + MarkdownDescription: "The maximum tokens per minute.", |
| 111 | + Computed: true, |
| 112 | + }, |
| 113 | + "max_images_per_1_minute": schema.Int64Attribute{ |
| 114 | + MarkdownDescription: "The maximum images per minute. Only present for relevant models.", |
| 115 | + Computed: true, |
| 116 | + }, |
| 117 | + "max_audio_megabytes_per_1_minute": schema.Int64Attribute{ |
| 118 | + MarkdownDescription: "The maximum audio megabytes per minute. Only present for relevant models.", |
| 119 | + Computed: true, |
| 120 | + }, |
| 121 | + "max_requests_per_1_day": schema.Int64Attribute{ |
| 122 | + MarkdownDescription: "The maximum requests per day. Only present for relevant models.", |
| 123 | + Computed: true, |
| 124 | + }, |
| 125 | + "batch_1_day_max_input_tokens": schema.Int64Attribute{ |
| 126 | + MarkdownDescription: "The maximum batch input tokens per day. Only present for relevant models.", |
| 127 | + Computed: true, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (d *ProjectRateLimitsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 137 | + var data ProjectRateLimitsDataSourceModel |
| 138 | + |
| 139 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 140 | + |
| 141 | + if resp.Diagnostics.HasError() { |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + var rateLimits []apiclient.ProjectRateLimit |
| 146 | + params := &apiclient.ListProjectRateLimitsParams{ |
| 147 | + Limit: ptr.Ptr(100), |
| 148 | + } |
| 149 | + |
| 150 | + for { |
| 151 | + httpResp, err := d.client.ListProjectRateLimitsWithResponse( |
| 152 | + ctx, |
| 153 | + data.ProjectId.ValueString(), |
| 154 | + params, |
| 155 | + ) |
| 156 | + if err != nil { |
| 157 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err)) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + if httpResp.StatusCode() != http.StatusOK { |
| 162 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body))) |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + rateLimits = append(rateLimits, httpResp.JSON200.Data...) |
| 167 | + |
| 168 | + if !httpResp.JSON200.HasMore { |
| 169 | + break |
| 170 | + } |
| 171 | + |
| 172 | + params.After = &httpResp.JSON200.LastId |
| 173 | + } |
| 174 | + |
| 175 | + if err := data.Fill(rateLimits); err != nil { |
| 176 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to fill data: %s", err)) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 181 | +} |
0 commit comments