Skip to main content

unitycatalog_common/models/resources/
mod.rs

1pub use super::_gen::ObjectLabel;
2pub use olai_store::{EMPTY_RESOURCE_NAME, ResourceName, ResourceRef};
3
4// Re-export the resource_name! macro from name.rs (which wraps the derive macro)
5pub use name::resource_name;
6
7mod name;
8
9/// Resource that a policy can authorize.
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub enum ResourceIdent {
12    Agent(ResourceRef),
13    AgentSkill(ResourceRef),
14    Share(ResourceRef),
15    Credential(ResourceRef),
16    ExternalLocation(ResourceRef),
17    Catalog(ResourceRef),
18    Schema(ResourceRef),
19    Table(ResourceRef),
20    Recipient(ResourceRef),
21    Provider(ResourceRef),
22    Column(ResourceRef),
23    Volume(ResourceRef),
24    Function(ResourceRef),
25    TagPolicy(ResourceRef),
26    StagingTable(ResourceRef),
27}
28
29impl ResourceIdent {
30    pub fn label(&self) -> &ObjectLabel {
31        self.as_ref()
32    }
33
34    pub fn reference(&self) -> &ResourceRef {
35        self.as_ref()
36    }
37
38    pub fn agent(name: impl Into<ResourceRef>) -> Self {
39        Self::Agent(name.into())
40    }
41
42    pub fn agent_skill(name: impl Into<ResourceRef>) -> Self {
43        Self::AgentSkill(name.into())
44    }
45
46    pub fn share(name: impl Into<ResourceRef>) -> Self {
47        Self::Share(name.into())
48    }
49
50    pub fn credential(name: impl Into<ResourceRef>) -> Self {
51        Self::Credential(name.into())
52    }
53
54    pub fn catalog(name: impl Into<ResourceRef>) -> Self {
55        Self::Catalog(name.into())
56    }
57
58    pub fn schema(name: impl Into<ResourceRef>) -> Self {
59        Self::Schema(name.into())
60    }
61
62    pub fn table(name: impl Into<ResourceRef>) -> Self {
63        Self::Table(name.into())
64    }
65
66    pub fn column(name: impl Into<ResourceRef>) -> Self {
67        Self::Column(name.into())
68    }
69
70    pub fn external_location(name: impl Into<ResourceRef>) -> Self {
71        Self::ExternalLocation(name.into())
72    }
73
74    pub fn recipient(name: impl Into<ResourceRef>) -> Self {
75        Self::Recipient(name.into())
76    }
77
78    pub fn provider(name: impl Into<ResourceRef>) -> Self {
79        Self::Provider(name.into())
80    }
81
82    pub fn volume(name: impl Into<ResourceRef>) -> Self {
83        Self::Volume(name.into())
84    }
85
86    pub fn function(name: impl Into<ResourceRef>) -> Self {
87        Self::Function(name.into())
88    }
89
90    pub fn tag_policy(name: impl Into<ResourceRef>) -> Self {
91        Self::TagPolicy(name.into())
92    }
93
94    pub fn staging_table(name: impl Into<ResourceRef>) -> Self {
95        Self::StagingTable(name.into())
96    }
97}
98
99impl std::fmt::Display for ResourceIdent {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        match self {
102            ResourceIdent::Agent(r) => write!(f, "agent:{}", r),
103            ResourceIdent::AgentSkill(r) => write!(f, "agent_skill:{}", r),
104            ResourceIdent::Share(r) => write!(f, "share:{}", r),
105            ResourceIdent::Credential(r) => write!(f, "credential:{}", r),
106            ResourceIdent::ExternalLocation(r) => write!(f, "external_location:{}", r),
107            ResourceIdent::Catalog(r) => write!(f, "catalog:{}", r),
108            ResourceIdent::Schema(r) => write!(f, "schema:{}", r),
109            ResourceIdent::Table(r) => write!(f, "table:{}", r),
110            ResourceIdent::Recipient(r) => write!(f, "recipient:{}", r),
111            ResourceIdent::Provider(r) => write!(f, "provider:{}", r),
112            ResourceIdent::Column(r) => write!(f, "column:{}", r),
113            ResourceIdent::Volume(r) => write!(f, "volume:{}", r),
114            ResourceIdent::Function(r) => write!(f, "function:{}", r),
115            ResourceIdent::TagPolicy(r) => write!(f, "tag_policy:{}", r),
116            ResourceIdent::StagingTable(r) => write!(f, "staging_table:{}", r),
117        }
118    }
119}
120
121impl AsRef<ResourceRef> for ResourceIdent {
122    fn as_ref(&self) -> &ResourceRef {
123        match self {
124            ResourceIdent::Agent(r) => r,
125            ResourceIdent::AgentSkill(r) => r,
126            ResourceIdent::Share(r) => r,
127            ResourceIdent::Credential(r) => r,
128            ResourceIdent::ExternalLocation(r) => r,
129            ResourceIdent::Catalog(r) => r,
130            ResourceIdent::Schema(r) => r,
131            ResourceIdent::Table(r) => r,
132            ResourceIdent::Recipient(r) => r,
133            ResourceIdent::Provider(r) => r,
134            ResourceIdent::Column(r) => r,
135            ResourceIdent::Volume(r) => r,
136            ResourceIdent::Function(r) => r,
137            ResourceIdent::TagPolicy(r) => r,
138            ResourceIdent::StagingTable(r) => r,
139        }
140    }
141}
142
143impl AsRef<ObjectLabel> for ResourceIdent {
144    fn as_ref(&self) -> &ObjectLabel {
145        match self {
146            ResourceIdent::Agent(_) => &ObjectLabel::Agent,
147            ResourceIdent::AgentSkill(_) => &ObjectLabel::AgentSkill,
148            ResourceIdent::Share(_) => &ObjectLabel::Share,
149            ResourceIdent::Credential(_) => &ObjectLabel::Credential,
150            ResourceIdent::ExternalLocation(_) => &ObjectLabel::ExternalLocation,
151            ResourceIdent::Catalog(_) => &ObjectLabel::Catalog,
152            ResourceIdent::Schema(_) => &ObjectLabel::Schema,
153            ResourceIdent::Table(_) => &ObjectLabel::Table,
154            ResourceIdent::Recipient(_) => &ObjectLabel::Recipient,
155            ResourceIdent::Provider(_) => &ObjectLabel::Provider,
156            ResourceIdent::Column(_) => &ObjectLabel::Column,
157            ResourceIdent::Volume(_) => &ObjectLabel::Volume,
158            ResourceIdent::Function(_) => &ObjectLabel::Function,
159            ResourceIdent::TagPolicy(_) => &ObjectLabel::TagPolicy,
160            ResourceIdent::StagingTable(_) => &ObjectLabel::StagingTable,
161        }
162    }
163}
164
165impl From<ResourceIdent> for ResourceRef {
166    fn from(ident: ResourceIdent) -> Self {
167        match ident {
168            ResourceIdent::Agent(r) => r,
169            ResourceIdent::AgentSkill(r) => r,
170            ResourceIdent::Share(r) => r,
171            ResourceIdent::Credential(r) => r,
172            ResourceIdent::ExternalLocation(r) => r,
173            ResourceIdent::Catalog(r) => r,
174            ResourceIdent::Schema(r) => r,
175            ResourceIdent::Table(r) => r,
176            ResourceIdent::Recipient(r) => r,
177            ResourceIdent::Provider(r) => r,
178            ResourceIdent::Column(r) => r,
179            ResourceIdent::Volume(r) => r,
180            ResourceIdent::Function(r) => r,
181            ResourceIdent::TagPolicy(r) => r,
182            ResourceIdent::StagingTable(r) => r,
183        }
184    }
185}
186
187impl From<&ResourceIdent> for ResourceRef {
188    fn from(ident: &ResourceIdent) -> Self {
189        (ident as &dyn AsRef<ResourceRef>).as_ref().clone()
190    }
191}
192
193impl From<&ResourceIdent> for ObjectLabel {
194    fn from(ident: &ResourceIdent) -> Self {
195        *(ident as &dyn AsRef<ObjectLabel>).as_ref()
196    }
197}
198
199impl From<ResourceIdent> for ObjectLabel {
200    fn from(ident: ResourceIdent) -> Self {
201        (&ident).into()
202    }
203}
204
205pub trait ResourceExt {
206    /// Get the name of the resource
207    fn resource_name(&self) -> ResourceName;
208
209    /// Get the reference for the resource
210    ///
211    /// Depending on the resource type, this may be a UUID or a name.
212    /// If possible, implementations should prefer to use the UUID
213    /// as it is globally unique. However not all resource-like objects
214    /// have a UUID field, or the UUID field may be optional.
215    fn resource_ref(&self) -> ResourceRef;
216
217    /// Get the ident for the resource
218    fn resource_ident(&self) -> ResourceIdent;
219}
220
221impl<T: ResourceExt> From<&T> for ResourceIdent {
222    fn from(resource: &T) -> Self {
223        resource.resource_ident()
224    }
225}
226
227impl ObjectLabel {
228    pub fn to_ident(&self, id: impl Into<ResourceRef>) -> ResourceIdent {
229        match self {
230            ObjectLabel::Agent => ResourceIdent::agent(id),
231            ObjectLabel::AgentSkill => ResourceIdent::agent_skill(id),
232            ObjectLabel::Share => ResourceIdent::share(id),
233            ObjectLabel::Credential => ResourceIdent::credential(id),
234            ObjectLabel::Catalog => ResourceIdent::catalog(id),
235            ObjectLabel::Schema => ResourceIdent::schema(id),
236            ObjectLabel::Table => ResourceIdent::table(id),
237            ObjectLabel::ExternalLocation => ResourceIdent::external_location(id),
238            ObjectLabel::Recipient => ResourceIdent::recipient(id),
239            ObjectLabel::Provider => ResourceIdent::provider(id),
240            ObjectLabel::Column => ResourceIdent::column(id),
241            ObjectLabel::Volume => ResourceIdent::volume(id),
242            ObjectLabel::Function => ResourceIdent::function(id),
243            ObjectLabel::TagPolicy => ResourceIdent::tag_policy(id),
244            ObjectLabel::StagingTable => ResourceIdent::staging_table(id),
245        }
246    }
247}